1use crate::types::{Color, ColorComponent};
4
5pub const BLACK: Color = [0.0, 0.0, 0.0, 1.0];
7pub const BLUE: Color = [0.0, 0.0, 1.0, 1.0];
9pub const CYAN: Color = [0.0, 1.0, 1.0, 1.0];
11pub const GRAY: Color = [0.5, 0.5, 0.5, 1.0];
13pub const GREEN: Color = [0.0, 0.5, 0.0, 1.0];
15pub const LIME: Color = [0.0, 1.0, 0.0, 1.0];
17pub const MAGENTA: Color = [1.0, 0.0, 1.0, 1.0];
19pub const MAROON: Color = [0.5, 0.0, 0.0, 1.0];
21pub const NAVY: Color = [0.0, 0.0, 0.5, 1.0];
23pub const OLIVE: Color = [0.5, 0.5, 0.0, 1.0];
25pub const PURPLE: Color = [0.5, 0.0, 0.5, 1.0];
27pub const RED: Color = [1.0, 0.0, 0.0, 1.0];
29pub const SILVER: Color = [0.75, 0.75, 0.75, 1.0];
31pub const TEAL: Color = [0.0, 0.5, 0.5, 1.0];
33pub const WHITE: Color = [1.0, 1.0, 1.0, 1.0];
35pub const YELLOW: Color = [1.0, 1.0, 0.0, 1.0];
37pub const TRANSPARENT: Color = [0.0; 4];
39
40pub fn grey(f: ColorComponent) -> Color {
44 [f, f, f, 1.0]
45}
46
47pub fn alpha(f: ColorComponent) -> Color {
49 [1.0, 1.0, 1.0, f]
50}
51
52pub 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
79pub 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
101pub 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}