Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweak some comments and add a new from_hex_alpha function #841

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,25 @@ impl Color {
Self::from_rgba(bytes[1], bytes[2], bytes[3], 255)
}

/// Build a color from a hexadecimal u32 and an accompanying u8 alpha value.
///
/// # Example
///
/// ```
/// use macroquad::prelude::*;
///
/// let light_blue = Color::from_hex_alpha(0x3CA7D5, 0x80);
/// assert_eq!(light_blue.r, 0.23529412);
/// assert_eq!(light_blue.g, 0.654902);
/// assert_eq!(light_blue.b, 0.8352941);
/// assert_eq!(light_blue.a, 0.5019608);
/// ```
pub fn from_hex_alpha(hex: u32, alpha: u8) -> Color {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not have very strong opinion here, but it looks like #rrggbbaa notation is fairly common. Maybe we should follow it to? Keep from_hex #rrggbb for the sake of backwards compatibility, while the new one will be 0xRRGGBBAA?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops-- totally forgot that 32 bits has room for alpha. Will patch that in, stupid oversight on my part.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It got room, the problem is that 0xff0000 is either red in from_hex or very transparent green in from_hex_alpha, which might be confusing. 0xAARRGGBB is a bit less error prone, but with #rrggbbaa being popular - even more confusing. So hex: u32, alpha: u8 actually does makes sense to me!

let bytes: [u8; 4] = hex.to_be_bytes();

Self::from_rgba(bytes[1], bytes[2], bytes[3], alpha)
}

/// Create a vec4 of red, green, blue, and alpha components.
pub fn to_vec(&self) -> glam::Vec4 {
glam::Vec4::new(self.r, self.g, self.b, self.a)
Expand Down
6 changes: 3 additions & 3 deletions src/time.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Cross platform system time access and FPS counters.
//! Cross-platform system time access and FPS counters.

use crate::get_context;

Expand All @@ -9,7 +9,7 @@ pub fn get_fps() -> i32 {
(1. / context.frame_time) as i32
}

/// Returns duration in seconds of the last frame drawn
/// Returns duration in seconds of the last frame drawn, the "delta time."
pub fn get_frame_time() -> f32 {
let context = get_context();

Expand All @@ -23,7 +23,7 @@ pub fn get_frame_time() -> f32 {
/// Returns elapsed wall-clock time in seconds since start
///
/// Note that as real world time progresses during computation,
/// the value returned will change. Therefore if you want
/// the value returned will change. Therefore, if you want
/// your game logic update to happen at the same *in-game* time
/// for all game objects, you should call this function once
/// save the value and reuse it throughout your code.
Expand Down