@@ -88,10 +88,35 @@ impl Color {
8888 }
8989 }
9090
91+ /// Creates a [`Color`] from its linear RGBA components.
92+ pub fn from_linear_rgba ( r : f32 , g : f32 , b : f32 , a : f32 ) -> Self {
93+ // As described in:
94+ // https://en.wikipedia.org/wiki/SRGB
95+ fn gamma_component ( u : f32 ) -> f32 {
96+ if u < 0.0031308 {
97+ 12.92 * u
98+ } else {
99+ 1.055 * u. powf ( 1.0 / 2.4 ) - 0.055
100+ }
101+ }
102+
103+ Self {
104+ r : gamma_component ( r) ,
105+ g : gamma_component ( g) ,
106+ b : gamma_component ( b) ,
107+ a,
108+ }
109+ }
110+
91111 /// Parses a [`Color`] from a hex string.
92112 ///
93- /// Supported formats are #rrggbb, #rrggbbaa, #rgb, and #rgba.
113+ /// Supported formats are ` #rrggbb`, ` #rrggbbaa`, ` #rgb` , and ` #rgba` .
94114 /// The starting "#" is optional. Both uppercase and lowercase are supported.
115+ ///
116+ /// If you have a static color string, using the [`color!`] macro should be preferred
117+ /// since it leverages hexadecimal literal notation and arithmetic directly.
118+ ///
119+ /// [`color!`]: crate::color!
95120 pub fn parse ( s : & str ) -> Option < Color > {
96121 let hex = s. strip_prefix ( '#' ) . unwrap_or ( s) ;
97122
@@ -130,26 +155,6 @@ impl Color {
130155 } )
131156 }
132157
133- /// Creates a [`Color`] from its linear RGBA components.
134- pub fn from_linear_rgba ( r : f32 , g : f32 , b : f32 , a : f32 ) -> Self {
135- // As described in:
136- // https://en.wikipedia.org/wiki/SRGB
137- fn gamma_component ( u : f32 ) -> f32 {
138- if u < 0.0031308 {
139- 12.92 * u
140- } else {
141- 1.055 * u. powf ( 1.0 / 2.4 ) - 0.055
142- }
143- }
144-
145- Self {
146- r : gamma_component ( r) ,
147- g : gamma_component ( g) ,
148- b : gamma_component ( b) ,
149- a,
150- }
151- }
152-
153158 /// Converts the [`Color`] into its RGBA8 equivalent.
154159 #[ must_use]
155160 pub fn into_rgba8 ( self ) -> [ u8 ; 4 ] {
0 commit comments