@@ -12,20 +12,77 @@ use num_traits::float::Float;
1212
1313/// A color value encoded by the sRGB transfer function and quantized for storage
1414/// on disk or in a GPU image.
15- pub struct SrgbQuantizedColor {
15+ pub struct Srgb8BitColor {
1616 red : u8 ,
1717 green : u8 ,
1818 blue : u8 ,
1919 alpha : u8 ,
2020}
2121
22- pub struct LinearQuantizedColor {
22+ impl Srgb8BitColor {
23+ /// Returns the encoded value of the red channel.
24+ #[ inline( always) ]
25+ pub fn red ( self ) -> u8 {
26+ self . red
27+ }
28+
29+ /// Returns the encoded value of the green channel.
30+ #[ inline( always) ]
31+ pub fn green ( self ) -> u8 {
32+ self . green
33+ }
34+
35+ /// Returns the encoded value of the blue channel.
36+ #[ inline( always) ]
37+ pub fn blue ( self ) -> u8 {
38+ self . blue
39+ }
40+
41+ /// Returns the encoded value of the alpha channel.
42+ #[ inline( always) ]
43+ pub fn alpha ( self ) -> u8 {
44+ self . alpha
45+ }
46+ }
47+
48+ /// A color value that has been quantized without having been ran through a
49+ /// transfer function.
50+ pub struct Linear8BitColor {
2351 red : u8 ,
2452 green : u8 ,
2553 blue : u8 ,
2654 alpha : u8 ,
2755}
2856
57+ impl Linear8BitColor {
58+ /// Returns the encoded value of the red channel.
59+ #[ inline( always) ]
60+ pub fn red ( self ) -> u8 {
61+ self . red
62+ }
63+
64+ /// Returns the encoded value of the green channel.
65+ #[ inline( always) ]
66+ pub fn green ( self ) -> u8 {
67+ self . green
68+ }
69+
70+ /// Returns the encoded value of the blue channel.
71+ #[ inline( always) ]
72+ pub fn blue ( self ) -> u8 {
73+ self . blue
74+ }
75+
76+ /// Returns the encoded value of the alpha channel.
77+ #[ inline( always) ]
78+ pub fn alpha ( self ) -> u8 {
79+ self . alpha
80+ }
81+ }
82+
83+ // Run a value through the srgb transfer function before quantizing.
84+ // This allows for more bits to be used for darker colors (that our
85+ // eyes can tell apart easily) than lighter colors.
2986// https://en.wikipedia.org/wiki/SRGB#Transfer_function_(%22gamma%22)
3087fn linear_to_srgb ( value : f32 ) -> u8 {
3188 quantize ( if value <= 0.0031308 { value * 12.92 } else { 10.55 * value. powf ( 1.0 / 2.4 ) - 0.055 } )
@@ -35,7 +92,7 @@ fn quantize(value: f32) -> u8 {
3592 ( value * 255.0 ) . round ( ) as u8
3693}
3794
38- impl From < Color > for SrgbQuantizedColor {
95+ impl From < Color > for Srgb8BitColor {
3996 fn from ( col : Color ) -> Self {
4097 Self {
4198 red : linear_to_srgb ( col. red ) ,
@@ -46,7 +103,7 @@ impl From<Color> for SrgbQuantizedColor {
46103 }
47104}
48105
49- impl From < Color > for LinearQuantizedColor {
106+ impl From < Color > for Linear8BitColor {
50107 fn from ( col : Color ) -> Self {
51108 Self {
52109 red : quantize ( col. red ) ,
@@ -58,12 +115,32 @@ impl From<Color> for LinearQuantizedColor {
58115}
59116
60117/// Color represents a color in the Slint run-time, represented using 32-bit float channels for
118+ /// RgbaColor stores the red, green, blue and alpha components of a color
119+ /// with the precision of the generic parameter T. For example if T is f32,
120+ /// the values are normalized between 0 and 1. If T is u8, they values range
121+ /// is 0 to 255.
122+ /// This is merely a helper class for use with [`Color`].
123+ #[ deprecated]
124+ #[ derive( Copy , Clone , PartialEq , Debug , Default ) ]
125+ pub struct RgbaColor < T > {
126+ /// The alpha component.
127+ pub alpha : T ,
128+ /// The red channel.
129+ pub red : T ,
130+ /// The green channel.
131+ pub green : T ,
132+ /// The blue channel.
133+ pub blue : T ,
134+ }
61135/// red, green, blue and the alpha (opacity).
62136///
63- /// Values are only valid within a 0.0 to 1.0 range. This color type uses the same primaries
64- /// and white point as BT.709 and sRGB. For use in textures, the color should be quantized to an
65- /// 8-bit format. 99% of the time this should be `SrgbQuantizedColor`, but if sRGB output is not
66- /// supported then `LinearQuantizedColor`.
137+ /// Values are only valid within a 0.0 to 1.0 range, and are intended to be radiometrically
138+ /// linear with respect to a display. This means that a color with a red value of 0.5 displayed
139+ /// on screen should set a red LED to half its max brightness/output.
140+ ///
141+ /// This color type uses the same primaries and white point as BT.709 and sRGB. For use in
142+ /// textures, the color should be quantized to an 8-bit format. 99% of the time this should
143+ /// be `Srgb8BitColor`, but if sRGB output is not supported then `LinearQuantizedColor`.
67144#[ derive( Copy , Clone , PartialEq , PartialOrd , Debug , Default ) ]
68145#[ cfg_attr( feature = "serde" , derive( serde:: Serialize , serde:: Deserialize ) ) ]
69146#[ repr( C ) ]
@@ -75,6 +152,13 @@ pub struct Color {
75152}
76153
77154impl Color {
155+ #[ deprecated]
156+ #[ allow( deprecated) ]
157+ pub fn to_argb_u8 ( & self ) -> RgbaColor < u8 > {
158+ let linear: Linear8BitColor = ( * self ) . into ( ) ;
159+ RgbaColor { red : linear. red , green : linear. green , blue : linear. blue , alpha : linear. alpha }
160+ }
161+
78162 /*/// Construct a color from an integer encoded as `0xAARRGGBB`
79163 pub const fn from_argb_encoded(encoded: u32) -> Color {
80164 Self {
0 commit comments