Skip to content

Commit ca01bc5

Browse files
committed
Add back stuff but as deprecated
1 parent 9e4611e commit ca01bc5

File tree

4 files changed

+99
-12
lines changed

4 files changed

+99
-12
lines changed

api/rs/slint/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ pub use i_slint_core::component_factory::ComponentFactory;
219219
pub use i_slint_core::graphics::{BorrowedOpenGLTextureBuilder, BorrowedOpenGLTextureOrigin};
220220
// keep in sync with internal/interpreter/api.rs
221221
pub use i_slint_core::graphics::{
222-
Brush, Color, Image, LoadImageError, Rgb8Pixel, Rgba8Pixel, SharedPixelBuffer,
222+
Brush, Color, Image, LoadImageError, Rgb8Pixel, Rgba8Pixel, RgbaColor, SharedPixelBuffer,
223223
};
224224
pub use i_slint_core::model::{
225225
FilterModel, MapModel, Model, ModelExt, ModelNotify, ModelPeer, ModelRc, ModelTracker,

internal/core/graphics/color.rs

Lines changed: 92 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
3087
fn 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

77154
impl 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 {

internal/core/graphics/image.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,11 @@ impl<Pixel: Clone> SharedPixelBuffer<Pixel> {
148148

149149
/// Convenience alias for a pixel with three color channels (red, green and blue), each
150150
/// encoded as u8.
151+
#[deprecated]
151152
pub type Rgb8Pixel = rgb::RGB8;
152153
/// Convenience alias for a pixel with four color channels (red, green, blue and alpha), each
153154
/// encoded as u8.
155+
#[deprecated]
154156
pub type Rgba8Pixel = rgb::RGBA8;
155157

156158
/// SharedImageBuffer is a container for images that are stored in CPU accessible memory.
@@ -468,7 +470,6 @@ impl ImageInner {
468470
ImageInner::StaticTextures(ts) => {
469471
let mut buffer =
470472
SharedPixelBuffer::<Rgba8Pixel>::new(ts.size.width, ts.size.height);
471-
/*
472473
let stride = buffer.width() as usize;
473474
let slice = buffer.make_mut_slice();
474475
for t in ts.textures.iter() {
@@ -525,8 +526,7 @@ impl ImageInner {
525526
}
526527
};
527528
}
528-
529-
} */
529+
}
530530
Some(SharedImageBuffer::RGBA8Premultiplied(buffer))
531531
}
532532
ImageInner::NineSlice(nine) => nine.0.render_to_buffer(None),

internal/core/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ pub use graphics::Color;
8080
#[doc(inline)]
8181
pub use graphics::Brush;
8282

83+
#[doc(inline)]
84+
pub use graphics::RgbaColor;
85+
8386
#[cfg(feature = "std")]
8487
#[doc(inline)]
8588
pub use graphics::PathData;

0 commit comments

Comments
 (0)