graphics/
color.rs

1//! Helper methods for colors
2
3use crate::types::{Color, ColorComponent};
4
5/// Black color.
6pub const BLACK: Color = [0.0, 0.0, 0.0, 1.0];
7/// Blue color.
8pub const BLUE: Color = [0.0, 0.0, 1.0, 1.0];
9/// Cyan color.
10pub const CYAN: Color = [0.0, 1.0, 1.0, 1.0];
11/// Gray color.
12pub const GRAY: Color = [0.5, 0.5, 0.5, 1.0];
13/// Green color.
14pub const GREEN: Color = [0.0, 0.5, 0.0, 1.0];
15/// Lime color.
16pub const LIME: Color = [0.0, 1.0, 0.0, 1.0];
17/// Magenta color.
18pub const MAGENTA: Color = [1.0, 0.0, 1.0, 1.0];
19/// Maroon color.
20pub const MAROON: Color = [0.5, 0.0, 0.0, 1.0];
21/// Navy color.
22pub const NAVY: Color = [0.0, 0.0, 0.5, 1.0];
23/// Olive color.
24pub const OLIVE: Color = [0.5, 0.5, 0.0, 1.0];
25/// Purple color.
26pub const PURPLE: Color = [0.5, 0.0, 0.5, 1.0];
27/// Red color.
28pub const RED: Color = [1.0, 0.0, 0.0, 1.0];
29/// Silver color.
30pub const SILVER: Color = [0.75, 0.75, 0.75, 1.0];
31/// Teal color.
32pub const TEAL: Color = [0.0, 0.5, 0.5, 1.0];
33/// White color.
34pub const WHITE: Color = [1.0, 1.0, 1.0, 1.0];
35/// Yellow color.
36pub const YELLOW: Color = [1.0, 1.0, 0.0, 1.0];
37/// Transparent color.
38pub const TRANSPARENT: Color = [0.0; 4];
39
40/// Returns a grey color
41/// # Arguments
42/// * `f` - The brightness of the color. 0.0 is black, 1.0 is white
43pub fn grey(f: ColorComponent) -> Color {
44    [f, f, f, 1.0]
45}
46
47/// Returns a semi-transparent white color
48pub fn alpha(f: ColorComponent) -> Color {
49    [1.0, 1.0, 1.0, f]
50}
51
52/// Converts from hexadecimal color format
53pub fn hex(hex: &str) -> Color {
54    use read_color::rgb_maybe_a;
55
56    let (rgb, a) = rgb_maybe_a(&mut hex.chars()).unwrap();
57    let color = match a {
58        None => [rgb[0], rgb[1], rgb[2], 255],
59        Some(a) => [rgb[0], rgb[1], rgb[2], a],
60    };
61    let inv_255 = 1.0f32 / 255.0f32;
62    [
63        color[0] as f32 * inv_255,
64        color[1] as f32 * inv_255,
65        color[2] as f32 * inv_255,
66        color[3] as f32 * inv_255,
67    ]
68}
69
70#[inline(always)]
71fn component_srgb_to_linear(f: ColorComponent) -> ColorComponent {
72    if f <= 0.04045 {
73        f / 12.92
74    } else {
75        ((f + 0.055) / 1.055).powf(2.4)
76    }
77}
78
79/// Converts gamma (brightness) from sRGB to linear color space.
80///
81/// sRGB is the default color space for image editors, pictures, internet etc.
82/// Linear gamma yields better results when doing math with colors.
83pub fn gamma_srgb_to_linear(c: Color) -> Color {
84    [
85        component_srgb_to_linear(c[0]),
86        component_srgb_to_linear(c[1]),
87        component_srgb_to_linear(c[2]),
88        c[3],
89    ]
90}
91
92#[inline(always)]
93fn component_linear_to_srgb(f: ColorComponent) -> ColorComponent {
94    if f <= 0.0031308 {
95        f * 12.92
96    } else {
97        1.055 * f.powf(1.0 / 2.4) - 0.055
98    }
99}
100
101/// Converts gamma (brightness) of a color from linear color space to sRGB.
102///
103/// sRGB is the default color space for image editors, pictures, internet etc.
104/// Linear gamma yields better results when doing math with colors.
105pub fn gamma_linear_to_srgb(c: Color) -> Color {
106    [
107        component_linear_to_srgb(c[0]),
108        component_linear_to_srgb(c[1]),
109        component_linear_to_srgb(c[2]),
110        c[3],
111    ]
112}