forge
Explore
Log in

Don't you dare go Hollow

Implement gradient color interpolation option (#548)

Browse files at this commit

3ace97660fde7fe1f0cc07a3925d1114af9a9c2fUnverifiedIvan Molodetskikh and K's ThinkpadParent: 0824737
* Added the better color averaging code (tested & functional)

* rustfmt

* Make Color f32 0..1, clarify premul/unpremul

* Fix imports and test name

* Premultiply gradient colors matching CSS

* Fix indentation

* fixup

* Add gradient image

---------
niri-config/src/lib.rsmodified+215 -43
@@ -410,8 +410,8 @@ impl Default for FocusRing {
410410 Self {
411411 off: false,
412412 width: FloatOrInt(4.),
413- active_color: Color::new(127, 200, 255, 255),
414- inactive_color: Color::new(80, 80, 80, 255),
413+ active_color: Color::from_rgba8_unpremul(127, 200, 255, 255),
414+ inactive_color: Color::from_rgba8_unpremul(80, 80, 80, 255),
415415 active_gradient: None,
416416 inactive_gradient: None,
417417 }
@@ -428,6 +428,8 @@ pub struct Gradient {
428428 pub angle: i16,
429429 #[knuffel(property, default)]
430430 pub relative_to: GradientRelativeTo,
431+ #[knuffel(property(name = "in"), str, default)]
432+ pub in_: GradientInterpolation,
431433 }
432434
433435 #[derive(knuffel::DecodeScalar, Debug, Default, Clone, Copy, PartialEq, Eq)]
@@ -437,6 +439,30 @@ pub enum GradientRelativeTo {
437439 WorkspaceView,
438440 }
439441
442+#[derive(Default, Debug, Clone, Copy, PartialEq)]
443+pub struct GradientInterpolation {
444+ pub color_space: GradientColorSpace,
445+ pub hue_interpolation: HueInterpolation,
446+}
447+
448+#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
449+pub enum GradientColorSpace {
450+ #[default]
451+ Srgb,
452+ SrgbLinear,
453+ Oklab,
454+ Oklch,
455+}
456+
457+#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
458+pub enum HueInterpolation {
459+ #[default]
460+ Shorter,
461+ Longer,
462+ Increasing,
463+ Decreasing,
464+}
465+
440466 #[derive(knuffel::Decode, Debug, Clone, Copy, PartialEq)]
441467 pub struct Border {
442468 #[knuffel(child)]
@@ -458,8 +484,8 @@ impl Default for Border {
458484 Self {
459485 off: true,
460486 width: FloatOrInt(4.),
461- active_color: Color::new(255, 200, 127, 255),
462- inactive_color: Color::new(80, 80, 80, 255),
487+ active_color: Color::from_rgba8_unpremul(255, 200, 127, 255),
488+ inactive_color: Color::from_rgba8_unpremul(80, 80, 80, 255),
463489 active_gradient: None,
464490 inactive_gradient: None,
465491 }
@@ -492,23 +518,49 @@ impl From<FocusRing> for Border {
492518 }
493519 }
494520
495-#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
521+/// RGB color in [0, 1] with unpremultiplied alpha.
522+#[derive(Debug, Default, Clone, Copy, PartialEq)]
496523 pub struct Color {
497- pub r: u8,
498- pub g: u8,
499- pub b: u8,
500- pub a: u8,
524+ pub r: f32,
525+ pub g: f32,
526+ pub b: f32,
527+ pub a: f32,
501528 }
502529
503530 impl Color {
504- pub const fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
531+ pub const fn new_unpremul(r: f32, g: f32, b: f32, a: f32) -> Self {
505532 Self { r, g, b, a }
506533 }
507-}
508534
509-impl From<Color> for [f32; 4] {
510- fn from(c: Color) -> Self {
511- let [r, g, b, a] = [c.r, c.g, c.b, c.a].map(|x| x as f32 / 255.);
535+ pub fn from_rgba8_unpremul(r: u8, g: u8, b: u8, a: u8) -> Self {
536+ Self::from_array_unpremul([r, g, b, a].map(|x| x as f32 / 255.))
537+ }
538+
539+ pub fn from_array_premul([r, g, b, a]: [f32; 4]) -> Self {
540+ let a = a.clamp(0., 1.);
541+
542+ if a == 0. {
543+ Self::new_unpremul(0., 0., 0., 0.)
544+ } else {
545+ Self {
546+ r: (r / a).clamp(0., 1.),
547+ g: (g / a).clamp(0., 1.),
548+ b: (b / a).clamp(0., 1.),
549+ a,
550+ }
551+ }
552+ }
553+
554+ pub fn from_array_unpremul([r, g, b, a]: [f32; 4]) -> Self {
555+ Self { r, g, b, a }
556+ }
557+
558+ pub fn to_array_unpremul(self) -> [f32; 4] {
559+ [self.r, self.g, self.b, self.a]
560+ }
561+
562+ pub fn to_array_premul(self) -> [f32; 4] {
563+ let [r, g, b, a] = [self.r, self.g, self.b, self.a];
512564 [r * a, g * a, b * a, a]
513565 }
514566 }
@@ -1429,12 +1481,73 @@ impl CornerRadius {
14291481 }
14301482 }
14311483
1484+impl FromStr for GradientInterpolation {
1485+ type Err = miette::Error;
1486+
1487+ fn from_str(s: &str) -> Result<Self, Self::Err> {
1488+ let mut iter = s.split_whitespace();
1489+ let in_part1 = iter.next();
1490+ let in_part2 = iter.next();
1491+ let in_part3 = iter.next();
1492+
1493+ let Some(in_part1) = in_part1 else {
1494+ return Err(miette!("missing color space"));
1495+ };
1496+
1497+ let color = match in_part1 {
1498+ "srgb" => GradientColorSpace::Srgb,
1499+ "srgb-linear" => GradientColorSpace::SrgbLinear,
1500+ "oklab" => GradientColorSpace::Oklab,
1501+ "oklch" => GradientColorSpace::Oklch,
1502+ x => {
1503+ return Err(miette!(
1504+ "invalid color space {x}; can be srgb, srgb-linear, oklab or oklch"
1505+ ))
1506+ }
1507+ };
1508+
1509+ let interpolation = if let Some(in_part2) = in_part2 {
1510+ if color != GradientColorSpace::Oklch {
1511+ return Err(miette!("only oklch color space can have hue interpolation"));
1512+ }
1513+
1514+ if in_part3 != Some("hue") {
1515+ return Err(miette!(
1516+ "interpolation must end with \"hue\", like \"oklch shorter hue\""
1517+ ));
1518+ } else if iter.next().is_some() {
1519+ return Err(miette!("unexpected text after hue interpolation"));
1520+ } else {
1521+ match in_part2 {
1522+ "shorter" => HueInterpolation::Shorter,
1523+ "longer" => HueInterpolation::Longer,
1524+ "increasing" => HueInterpolation::Increasing,
1525+ "decreasing" => HueInterpolation::Decreasing,
1526+ x => {
1527+ return Err(miette!(
1528+ "invalid hue interpolation {x}; \
1529+ can be shorter, longer, increasing, decreasing"
1530+ ))
1531+ }
1532+ }
1533+ }
1534+ } else {
1535+ HueInterpolation::default()
1536+ };
1537+
1538+ Ok(Self {
1539+ color_space: color,
1540+ hue_interpolation: interpolation,
1541+ })
1542+ }
1543+}
1544+
14321545 impl FromStr for Color {
14331546 type Err = miette::Error;
14341547
14351548 fn from_str(s: &str) -> Result<Self, Self::Err> {
1436- let [r, g, b, a] = csscolorparser::parse(s).into_diagnostic()?.to_rgba8();
1437- Ok(Self { r, g, b, a })
1549+ let color = csscolorparser::parse(s).into_diagnostic()?.to_array();
1550+ Ok(Self::from_array_unpremul(color.map(|x| x as f32)))
14381551 }
14391552 }
14401553
@@ -1453,7 +1566,7 @@ struct ColorRgba {
14531566 impl From<ColorRgba> for Color {
14541567 fn from(value: ColorRgba) -> Self {
14551568 let ColorRgba { r, g, b, a } = value;
1456- Self { r, g, b, a }
1569+ Self::from_array_unpremul([r, g, b, a].map(|x| x as f32 / 255.))
14571570 }
14581571 }
14591572
@@ -2771,41 +2884,25 @@ mod tests {
27712884 focus_ring: FocusRing {
27722885 off: false,
27732886 width: FloatOrInt(5.),
2774- active_color: Color {
2775- r: 0,
2776- g: 100,
2777- b: 200,
2778- a: 255,
2779- },
2780- inactive_color: Color {
2781- r: 255,
2782- g: 200,
2783- b: 100,
2784- a: 0,
2785- },
2887+ active_color: Color::from_rgba8_unpremul(0, 100, 200, 255),
2888+ inactive_color: Color::from_rgba8_unpremul(255, 200, 100, 0),
27862889 active_gradient: Some(Gradient {
2787- from: Color::new(10, 20, 30, 255),
2788- to: Color::new(0, 128, 255, 255),
2890+ from: Color::from_rgba8_unpremul(10, 20, 30, 255),
2891+ to: Color::from_rgba8_unpremul(0, 128, 255, 255),
27892892 angle: 180,
27902893 relative_to: GradientRelativeTo::WorkspaceView,
2894+ in_: GradientInterpolation {
2895+ color_space: GradientColorSpace::Srgb,
2896+ hue_interpolation: HueInterpolation::Shorter,
2897+ },
27912898 }),
27922899 inactive_gradient: None,
27932900 },
27942901 border: Border {
27952902 off: false,
27962903 width: FloatOrInt(3.),
2797- active_color: Color {
2798- r: 255,
2799- g: 200,
2800- b: 127,
2801- a: 255,
2802- },
2803- inactive_color: Color {
2804- r: 255,
2805- g: 200,
2806- b: 100,
2807- a: 0,
2808- },
2904+ active_color: Color::from_rgba8_unpremul(255, 200, 127, 255),
2905+ inactive_color: Color::from_rgba8_unpremul(255, 200, 100, 0),
28092906 active_gradient: None,
28102907 inactive_gradient: None,
28112908 },
@@ -3095,6 +3192,81 @@ mod tests {
30953192 assert!("10% ".parse::<SizeChange>().is_err());
30963193 }
30973194
3195+ #[test]
3196+ fn parse_gradient_interpolation() {
3197+ assert_eq!(
3198+ "srgb".parse::<GradientInterpolation>().unwrap(),
3199+ GradientInterpolation {
3200+ color_space: GradientColorSpace::Srgb,
3201+ ..Default::default()
3202+ }
3203+ );
3204+ assert_eq!(
3205+ "srgb-linear".parse::<GradientInterpolation>().unwrap(),
3206+ GradientInterpolation {
3207+ color_space: GradientColorSpace::SrgbLinear,
3208+ ..Default::default()
3209+ }
3210+ );
3211+ assert_eq!(
3212+ "oklab".parse::<GradientInterpolation>().unwrap(),
3213+ GradientInterpolation {
3214+ color_space: GradientColorSpace::Oklab,
3215+ ..Default::default()
3216+ }
3217+ );
3218+ assert_eq!(
3219+ "oklch".parse::<GradientInterpolation>().unwrap(),
3220+ GradientInterpolation {
3221+ color_space: GradientColorSpace::Oklch,
3222+ ..Default::default()
3223+ }
3224+ );
3225+ assert_eq!(
3226+ "oklch shorter hue"
3227+ .parse::<GradientInterpolation>()
3228+ .unwrap(),
3229+ GradientInterpolation {
3230+ color_space: GradientColorSpace::Oklch,
3231+ hue_interpolation: HueInterpolation::Shorter,
3232+ }
3233+ );
3234+ assert_eq!(
3235+ "oklch longer hue".parse::<GradientInterpolation>().unwrap(),
3236+ GradientInterpolation {
3237+ color_space: GradientColorSpace::Oklch,
3238+ hue_interpolation: HueInterpolation::Longer,
3239+ }
3240+ );
3241+ assert_eq!(
3242+ "oklch decreasing hue"
3243+ .parse::<GradientInterpolation>()
3244+ .unwrap(),
3245+ GradientInterpolation {
3246+ color_space: GradientColorSpace::Oklch,
3247+ hue_interpolation: HueInterpolation::Decreasing,
3248+ }
3249+ );
3250+ assert_eq!(
3251+ "oklch increasing hue"
3252+ .parse::<GradientInterpolation>()
3253+ .unwrap(),
3254+ GradientInterpolation {
3255+ color_space: GradientColorSpace::Oklch,
3256+ hue_interpolation: HueInterpolation::Increasing,
3257+ }
3258+ );
3259+
3260+ assert!("".parse::<GradientInterpolation>().is_err());
3261+ assert!("srgb shorter hue".parse::<GradientInterpolation>().is_err());
3262+ assert!("oklch shorter".parse::<GradientInterpolation>().is_err());
3263+ assert!("oklch shorter h".parse::<GradientInterpolation>().is_err());
3264+ assert!("oklch a hue".parse::<GradientInterpolation>().is_err());
3265+ assert!("oklch shorter hue a"
3266+ .parse::<GradientInterpolation>()
3267+ .is_err());
3268+ }
3269+
30983270 #[test]
30993271 fn parse_iso_level3_shift() {
31003272 assert_eq!(
niri-visual-tests/src/cases/gradient_angle.rsmodified+4 -3
@@ -4,7 +4,7 @@ use std::time::Duration;
44
55 use niri::animation::ANIMATION_SLOWDOWN;
66 use niri::render_helpers::border::BorderRenderElement;
7-use niri_config::CornerRadius;
7+use niri_config::{Color, CornerRadius, GradientInterpolation};
88 use smithay::backend::renderer::element::RenderElement;
99 use smithay::backend::renderer::gles::GlesRenderer;
1010 use smithay::utils::{Logical, Physical, Rectangle, Size};
@@ -64,8 +64,9 @@ impl TestCase for GradientAngle {
6464 [BorderRenderElement::new(
6565 area.size,
6666 Rectangle::from_loc_and_size((0., 0.), area.size),
67- [1., 0., 0., 1.],
68- [0., 1., 0., 1.],
67+ GradientInterpolation::default(),
68+ Color::new_unpremul(1., 0., 0., 1.),
69+ Color::new_unpremul(0., 1., 0., 1.),
6970 self.angle - FRAC_PI_2,
7071 Rectangle::from_loc_and_size((0., 0.), area.size),
7172 0.,
niri-visual-tests/src/cases/gradient_area.rsmodified+5 -4
@@ -5,7 +5,7 @@ use std::time::Duration;
55 use niri::animation::ANIMATION_SLOWDOWN;
66 use niri::layout::focus_ring::FocusRing;
77 use niri::render_helpers::border::BorderRenderElement;
8-use niri_config::{Color, CornerRadius, FloatOrInt};
8+use niri_config::{Color, CornerRadius, FloatOrInt, GradientInterpolation};
99 use smithay::backend::renderer::element::RenderElement;
1010 use smithay::backend::renderer::gles::GlesRenderer;
1111 use smithay::utils::{Logical, Physical, Point, Rectangle, Size};
@@ -23,7 +23,7 @@ impl GradientArea {
2323 let border = FocusRing::new(niri_config::FocusRing {
2424 off: false,
2525 width: FloatOrInt(1.),
26- active_color: Color::new(255, 255, 255, 128),
26+ active_color: Color::from_rgba8_unpremul(255, 255, 255, 128),
2727 inactive_color: Color::default(),
2828 active_gradient: None,
2929 inactive_gradient: None,
@@ -104,8 +104,9 @@ impl TestCase for GradientArea {
104104 [BorderRenderElement::new(
105105 area.size,
106106 g_area,
107- [1., 0., 0., 1.],
108- [0., 1., 0., 1.],
107+ GradientInterpolation::default(),
108+ Color::new_unpremul(1., 0., 0., 1.),
109+ Color::new_unpremul(0., 1., 0., 1.),
109110 FRAC_PI_4,
110111 Rectangle::from_loc_and_size((0, 0), rect_size).to_f64(),
111112 0.,
niri-visual-tests/src/cases/gradient_oklab.rsadded+53 -0
@@ -0,0 +1,53 @@
1+use niri::render_helpers::border::BorderRenderElement;
2+use niri_config::{
3+ Color, CornerRadius, GradientColorSpace, GradientInterpolation, HueInterpolation,
4+};
5+use smithay::backend::renderer::element::RenderElement;
6+use smithay::backend::renderer::gles::GlesRenderer;
7+use smithay::utils::{Logical, Physical, Rectangle, Size};
8+
9+use super::TestCase;
10+
11+pub struct GradientOklab {
12+ gradient_format: GradientInterpolation,
13+}
14+
15+impl GradientOklab {
16+ pub fn new(_size: Size<i32, Logical>) -> Self {
17+ Self {
18+ gradient_format: GradientInterpolation {
19+ color_space: GradientColorSpace::Oklab,
20+ hue_interpolation: HueInterpolation::Shorter,
21+ },
22+ }
23+ }
24+}
25+
26+impl TestCase for GradientOklab {
27+ fn render(
28+ &mut self,
29+ _renderer: &mut GlesRenderer,
30+ size: Size<i32, Physical>,
31+ ) -> Vec<Box<dyn RenderElement<GlesRenderer>>> {
32+ let (a, b) = (size.w / 6, size.h / 3);
33+ let size = (size.w - a * 2, size.h - b * 2);
34+ let area = Rectangle::from_loc_and_size((a, b), size).to_f64();
35+
36+ [BorderRenderElement::new(
37+ area.size,
38+ Rectangle::from_loc_and_size((0., 0.), area.size),
39+ self.gradient_format,
40+ Color::new_unpremul(1., 0., 0., 1.),
41+ Color::new_unpremul(0., 1., 0., 1.),
42+ 0.,
43+ Rectangle::from_loc_and_size((0., 0.), area.size),
44+ 0.,
45+ CornerRadius::default(),
46+ 1.,
47+ )
48+ .with_location(area.loc)]
49+ .into_iter()
50+ .map(|elem| Box::new(elem) as _)
51+ .collect()
52+ }
53+}
niri-visual-tests/src/cases/gradient_oklab_alpha.rsadded+51 -0
@@ -0,0 +1,51 @@
1+use niri::render_helpers::border::BorderRenderElement;
2+use niri_config::{Color, CornerRadius, GradientColorSpace, GradientInterpolation};
3+use smithay::backend::renderer::element::RenderElement;
4+use smithay::backend::renderer::gles::GlesRenderer;
5+use smithay::utils::{Logical, Physical, Rectangle, Size};
6+
7+use super::TestCase;
8+
9+pub struct GradientOklabAlpha {
10+ gradient_format: GradientInterpolation,
11+}
12+
13+impl GradientOklabAlpha {
14+ pub fn new(_size: Size<i32, Logical>) -> Self {
15+ Self {
16+ gradient_format: GradientInterpolation {
17+ color_space: GradientColorSpace::Oklab,
18+ hue_interpolation: Default::default(),
19+ },
20+ }
21+ }
22+}
23+
24+impl TestCase for GradientOklabAlpha {
25+ fn render(
26+ &mut self,
27+ _renderer: &mut GlesRenderer,
28+ size: Size<i32, Physical>,
29+ ) -> Vec<Box<dyn RenderElement<GlesRenderer>>> {
30+ let (a, b) = (size.w / 6, size.h / 3);
31+ let size = (size.w - a * 2, size.h - b * 2);
32+ let area = Rectangle::from_loc_and_size((a, b), size).to_f64();
33+
34+ [BorderRenderElement::new(
35+ area.size,
36+ Rectangle::from_loc_and_size((0., 0.), area.size),
37+ self.gradient_format,
38+ Color::new_unpremul(1., 0., 0., 1.),
39+ Color::new_unpremul(0., 1., 0., 0.),
40+ 0.,
41+ Rectangle::from_loc_and_size((0., 0.), area.size),
42+ 0.,
43+ CornerRadius::default(),
44+ 1.,
45+ )
46+ .with_location(area.loc)]
47+ .into_iter()
48+ .map(|elem| Box::new(elem) as _)
49+ .collect()
50+ }
51+}
niri-visual-tests/src/cases/gradient_oklch_alpha.rsadded+53 -0
@@ -0,0 +1,53 @@
1+use niri::render_helpers::border::BorderRenderElement;
2+use niri_config::{
3+ Color, CornerRadius, GradientColorSpace, GradientInterpolation, HueInterpolation,
4+};
5+use smithay::backend::renderer::element::RenderElement;
6+use smithay::backend::renderer::gles::GlesRenderer;
7+use smithay::utils::{Logical, Physical, Rectangle, Size};
8+
9+use super::TestCase;
10+
11+pub struct GradientOklchAlpha {
12+ gradient_format: GradientInterpolation,
13+}
14+
15+impl GradientOklchAlpha {
16+ pub fn new(_size: Size<i32, Logical>) -> Self {
17+ Self {
18+ gradient_format: GradientInterpolation {
19+ color_space: GradientColorSpace::Oklch,
20+ hue_interpolation: HueInterpolation::Longer,
21+ },
22+ }
23+ }
24+}
25+
26+impl TestCase for GradientOklchAlpha {
27+ fn render(
28+ &mut self,
29+ _renderer: &mut GlesRenderer,
30+ size: Size<i32, Physical>,
31+ ) -> Vec<Box<dyn RenderElement<GlesRenderer>>> {
32+ let (a, b) = (size.w / 6, size.h / 3);
33+ let size = (size.w - a * 2, size.h - b * 2);
34+ let area = Rectangle::from_loc_and_size((a, b), size).to_f64();
35+
36+ [BorderRenderElement::new(
37+ area.size,
38+ Rectangle::from_loc_and_size((0., 0.), area.size),
39+ self.gradient_format,
40+ Color::new_unpremul(1., 0., 0., 1.),
41+ Color::new_unpremul(0., 1., 0., 0.),
42+ 0.,
43+ Rectangle::from_loc_and_size((0., 0.), area.size),
44+ 0.,
45+ CornerRadius::default(),
46+ 1.,
47+ )
48+ .with_location(area.loc)]
49+ .into_iter()
50+ .map(|elem| Box::new(elem) as _)
51+ .collect()
52+ }
53+}
niri-visual-tests/src/cases/gradient_oklch_decreasing.rsadded+53 -0
@@ -0,0 +1,53 @@
1+use niri::render_helpers::border::BorderRenderElement;
2+use niri_config::{
3+ Color, CornerRadius, GradientColorSpace, GradientInterpolation, HueInterpolation,
4+};
5+use smithay::backend::renderer::element::RenderElement;
6+use smithay::backend::renderer::gles::GlesRenderer;
7+use smithay::utils::{Logical, Physical, Rectangle, Size};
8+
9+use super::TestCase;
10+
11+pub struct GradientOklchDecreasing {
12+ gradient_format: GradientInterpolation,
13+}
14+
15+impl GradientOklchDecreasing {
16+ pub fn new(_size: Size<i32, Logical>) -> Self {
17+ Self {
18+ gradient_format: GradientInterpolation {
19+ color_space: GradientColorSpace::Oklch,
20+ hue_interpolation: HueInterpolation::Decreasing,
21+ },
22+ }
23+ }
24+}
25+
26+impl TestCase for GradientOklchDecreasing {
27+ fn render(
28+ &mut self,
29+ _renderer: &mut GlesRenderer,
30+ size: Size<i32, Physical>,
31+ ) -> Vec<Box<dyn RenderElement<GlesRenderer>>> {
32+ let (a, b) = (size.w / 6, size.h / 3);
33+ let size = (size.w - a * 2, size.h - b * 2);
34+ let area = Rectangle::from_loc_and_size((a, b), size).to_f64();
35+
36+ [BorderRenderElement::new(
37+ area.size,
38+ Rectangle::from_loc_and_size((0., 0.), area.size),
39+ self.gradient_format,
40+ Color::new_unpremul(1., 0., 0., 1.),
41+ Color::new_unpremul(0., 1., 0., 1.),
42+ 0.,
43+ Rectangle::from_loc_and_size((0., 0.), area.size),
44+ 0.,
45+ CornerRadius::default(),
46+ 1.,
47+ )
48+ .with_location(area.loc)]
49+ .into_iter()
50+ .map(|elem| Box::new(elem) as _)
51+ .collect()
52+ }
53+}
niri-visual-tests/src/cases/gradient_oklch_increasing.rsadded+53 -0
@@ -0,0 +1,53 @@
1+use niri::render_helpers::border::BorderRenderElement;
2+use niri_config::{
3+ Color, CornerRadius, GradientColorSpace, GradientInterpolation, HueInterpolation,
4+};
5+use smithay::backend::renderer::element::RenderElement;
6+use smithay::backend::renderer::gles::GlesRenderer;
7+use smithay::utils::{Logical, Physical, Rectangle, Size};
8+
9+use super::TestCase;
10+
11+pub struct GradientOklchIncreasing {
12+ gradient_format: GradientInterpolation,
13+}
14+
15+impl GradientOklchIncreasing {
16+ pub fn new(_size: Size<i32, Logical>) -> Self {
17+ Self {
18+ gradient_format: GradientInterpolation {
19+ color_space: GradientColorSpace::Oklch,
20+ hue_interpolation: HueInterpolation::Increasing,
21+ },
22+ }
23+ }
24+}
25+
26+impl TestCase for GradientOklchIncreasing {
27+ fn render(
28+ &mut self,
29+ _renderer: &mut GlesRenderer,
30+ size: Size<i32, Physical>,
31+ ) -> Vec<Box<dyn RenderElement<GlesRenderer>>> {
32+ let (a, b) = (size.w / 6, size.h / 3);
33+ let size = (size.w - a * 2, size.h - b * 2);
34+ let area = Rectangle::from_loc_and_size((a, b), size).to_f64();
35+
36+ [BorderRenderElement::new(
37+ area.size,
38+ Rectangle::from_loc_and_size((0., 0.), area.size),
39+ self.gradient_format,
40+ Color::new_unpremul(1., 0., 0., 1.),
41+ Color::new_unpremul(0., 1., 0., 1.),
42+ 0.,
43+ Rectangle::from_loc_and_size((0., 0.), area.size),
44+ 0.,
45+ CornerRadius::default(),
46+ 1.,
47+ )
48+ .with_location(area.loc)]
49+ .into_iter()
50+ .map(|elem| Box::new(elem) as _)
51+ .collect()
52+ }
53+}
niri-visual-tests/src/cases/gradient_oklch_longer.rsadded+53 -0
@@ -0,0 +1,53 @@
1+use niri::render_helpers::border::BorderRenderElement;
2+use niri_config::{
3+ Color, CornerRadius, GradientColorSpace, GradientInterpolation, HueInterpolation,
4+};
5+use smithay::backend::renderer::element::RenderElement;
6+use smithay::backend::renderer::gles::GlesRenderer;
7+use smithay::utils::{Logical, Physical, Rectangle, Size};
8+
9+use super::TestCase;
10+
11+pub struct GradientOklchLonger {
12+ gradient_format: GradientInterpolation,
13+}
14+
15+impl GradientOklchLonger {
16+ pub fn new(_size: Size<i32, Logical>) -> Self {
17+ Self {
18+ gradient_format: GradientInterpolation {
19+ color_space: GradientColorSpace::Oklch,
20+ hue_interpolation: HueInterpolation::Longer,
21+ },
22+ }
23+ }
24+}
25+
26+impl TestCase for GradientOklchLonger {
27+ fn render(
28+ &mut self,
29+ _renderer: &mut GlesRenderer,
30+ size: Size<i32, Physical>,
31+ ) -> Vec<Box<dyn RenderElement<GlesRenderer>>> {
32+ let (a, b) = (size.w / 6, size.h / 3);
33+ let size = (size.w - a * 2, size.h - b * 2);
34+ let area = Rectangle::from_loc_and_size((a, b), size).to_f64();
35+
36+ [BorderRenderElement::new(
37+ area.size,
38+ Rectangle::from_loc_and_size((0., 0.), area.size),
39+ self.gradient_format,
40+ Color::new_unpremul(1., 0., 0., 1.),
41+ Color::new_unpremul(0., 1., 0., 1.),
42+ 0.,
43+ Rectangle::from_loc_and_size((0., 0.), area.size),
44+ 0.,
45+ CornerRadius::default(),
46+ 1.,
47+ )
48+ .with_location(area.loc)]
49+ .into_iter()
50+ .map(|elem| Box::new(elem) as _)
51+ .collect()
52+ }
53+}
niri-visual-tests/src/cases/gradient_oklch_shorter.rsadded+53 -0
@@ -0,0 +1,53 @@
1+use niri::render_helpers::border::BorderRenderElement;
2+use niri_config::{
3+ Color, CornerRadius, GradientColorSpace, GradientInterpolation, HueInterpolation,
4+};
5+use smithay::backend::renderer::element::RenderElement;
6+use smithay::backend::renderer::gles::GlesRenderer;
7+use smithay::utils::{Logical, Physical, Rectangle, Size};
8+
9+use super::TestCase;
10+
11+pub struct GradientOklchShorter {
12+ gradient_format: GradientInterpolation,
13+}
14+
15+impl GradientOklchShorter {
16+ pub fn new(_size: Size<i32, Logical>) -> Self {
17+ Self {
18+ gradient_format: GradientInterpolation {
19+ color_space: GradientColorSpace::Oklch,
20+ hue_interpolation: HueInterpolation::Shorter,
21+ },
22+ }
23+ }
24+}
25+
26+impl TestCase for GradientOklchShorter {
27+ fn render(
28+ &mut self,
29+ _renderer: &mut GlesRenderer,
30+ size: Size<i32, Physical>,
31+ ) -> Vec<Box<dyn RenderElement<GlesRenderer>>> {
32+ let (a, b) = (size.w / 6, size.h / 3);
33+ let size = (size.w - a * 2, size.h - b * 2);
34+ let area = Rectangle::from_loc_and_size((a, b), size).to_f64();
35+
36+ [BorderRenderElement::new(
37+ area.size,
38+ Rectangle::from_loc_and_size((0., 0.), area.size),
39+ self.gradient_format,
40+ Color::new_unpremul(1., 0., 0., 1.),
41+ Color::new_unpremul(0., 1., 0., 1.),
42+ 0.,
43+ Rectangle::from_loc_and_size((0., 0.), area.size),
44+ 0.,
45+ CornerRadius::default(),
46+ 1.,
47+ )
48+ .with_location(area.loc)]
49+ .into_iter()
50+ .map(|elem| Box::new(elem) as _)
51+ .collect()
52+ }
53+}
niri-visual-tests/src/cases/gradient_srgb.rsadded+53 -0
@@ -0,0 +1,53 @@
1+use niri::render_helpers::border::BorderRenderElement;
2+use niri_config::{
3+ Color, CornerRadius, GradientColorSpace, GradientInterpolation, HueInterpolation,
4+};
5+use smithay::backend::renderer::element::RenderElement;
6+use smithay::backend::renderer::gles::GlesRenderer;
7+use smithay::utils::{Logical, Physical, Rectangle, Size};
8+
9+use super::TestCase;
10+
11+pub struct GradientSrgb {
12+ gradient_format: GradientInterpolation,
13+}
14+
15+impl GradientSrgb {
16+ pub fn new(_size: Size<i32, Logical>) -> Self {
17+ Self {
18+ gradient_format: GradientInterpolation {
19+ color_space: GradientColorSpace::Srgb,
20+ hue_interpolation: HueInterpolation::Shorter,
21+ },
22+ }
23+ }
24+}
25+
26+impl TestCase for GradientSrgb {
27+ fn render(
28+ &mut self,
29+ _renderer: &mut GlesRenderer,
30+ size: Size<i32, Physical>,
31+ ) -> Vec<Box<dyn RenderElement<GlesRenderer>>> {
32+ let (a, b) = (size.w / 6, size.h / 3);
33+ let size = (size.w - a * 2, size.h - b * 2);
34+ let area = Rectangle::from_loc_and_size((a, b), size).to_f64();
35+
36+ [BorderRenderElement::new(
37+ area.size,
38+ Rectangle::from_loc_and_size((0., 0.), area.size),
39+ self.gradient_format,
40+ Color::new_unpremul(1., 0., 0., 1.),
41+ Color::new_unpremul(0., 1., 0., 1.),
42+ 0.,
43+ Rectangle::from_loc_and_size((0., 0.), area.size),
44+ 0.,
45+ CornerRadius::default(),
46+ 1.,
47+ )
48+ .with_location(area.loc)]
49+ .into_iter()
50+ .map(|elem| Box::new(elem) as _)
51+ .collect()
52+ }
53+}
niri-visual-tests/src/cases/gradient_srgb_alpha.rsadded+51 -0
@@ -0,0 +1,51 @@
1+use niri::render_helpers::border::BorderRenderElement;
2+use niri_config::{Color, CornerRadius, GradientColorSpace, GradientInterpolation};
3+use smithay::backend::renderer::element::RenderElement;
4+use smithay::backend::renderer::gles::GlesRenderer;
5+use smithay::utils::{Logical, Physical, Rectangle, Size};
6+
7+use super::TestCase;
8+
9+pub struct GradientSrgbAlpha {
10+ gradient_format: GradientInterpolation,
11+}
12+
13+impl GradientSrgbAlpha {
14+ pub fn new(_size: Size<i32, Logical>) -> Self {
15+ Self {
16+ gradient_format: GradientInterpolation {
17+ color_space: GradientColorSpace::Srgb,
18+ hue_interpolation: Default::default(),
19+ },
20+ }
21+ }
22+}
23+
24+impl TestCase for GradientSrgbAlpha {
25+ fn render(
26+ &mut self,
27+ _renderer: &mut GlesRenderer,
28+ size: Size<i32, Physical>,
29+ ) -> Vec<Box<dyn RenderElement<GlesRenderer>>> {
30+ let (a, b) = (size.w / 6, size.h / 3);
31+ let size = (size.w - a * 2, size.h - b * 2);
32+ let area = Rectangle::from_loc_and_size((a, b), size).to_f64();
33+
34+ [BorderRenderElement::new(
35+ area.size,
36+ Rectangle::from_loc_and_size((0., 0.), area.size),
37+ self.gradient_format,
38+ Color::new_unpremul(1., 0., 0., 1.),
39+ Color::new_unpremul(0., 1., 0., 0.),
40+ 0.,
41+ Rectangle::from_loc_and_size((0., 0.), area.size),
42+ 0.,
43+ CornerRadius::default(),
44+ 1.,
45+ )
46+ .with_location(area.loc)]
47+ .into_iter()
48+ .map(|elem| Box::new(elem) as _)
49+ .collect()
50+ }
51+}
niri-visual-tests/src/cases/gradient_srgblinear.rsadded+53 -0
@@ -0,0 +1,53 @@
1+use niri::render_helpers::border::BorderRenderElement;
2+use niri_config::{
3+ Color, CornerRadius, GradientColorSpace, GradientInterpolation, HueInterpolation,
4+};
5+use smithay::backend::renderer::element::RenderElement;
6+use smithay::backend::renderer::gles::GlesRenderer;
7+use smithay::utils::{Logical, Physical, Rectangle, Size};
8+
9+use super::TestCase;
10+
11+pub struct GradientSrgbLinear {
12+ gradient_format: GradientInterpolation,
13+}
14+
15+impl GradientSrgbLinear {
16+ pub fn new(_size: Size<i32, Logical>) -> Self {
17+ Self {
18+ gradient_format: GradientInterpolation {
19+ color_space: GradientColorSpace::SrgbLinear,
20+ hue_interpolation: HueInterpolation::Shorter,
21+ },
22+ }
23+ }
24+}
25+
26+impl TestCase for GradientSrgbLinear {
27+ fn render(
28+ &mut self,
29+ _renderer: &mut GlesRenderer,
30+ size: Size<i32, Physical>,
31+ ) -> Vec<Box<dyn RenderElement<GlesRenderer>>> {
32+ let (a, b) = (size.w / 6, size.h / 3);
33+ let size = (size.w - a * 2, size.h - b * 2);
34+ let area = Rectangle::from_loc_and_size((a, b), size).to_f64();
35+
36+ [BorderRenderElement::new(
37+ area.size,
38+ Rectangle::from_loc_and_size((0., 0.), area.size),
39+ self.gradient_format,
40+ Color::new_unpremul(1., 0., 0., 1.),
41+ Color::new_unpremul(0., 1., 0., 1.),
42+ 0.,
43+ Rectangle::from_loc_and_size((0., 0.), area.size),
44+ 0.,
45+ CornerRadius::default(),
46+ 1.,
47+ )
48+ .with_location(area.loc)]
49+ .into_iter()
50+ .map(|elem| Box::new(elem) as _)
51+ .collect()
52+ }
53+}
niri-visual-tests/src/cases/gradient_srgblinear_alpha.rsadded+51 -0
@@ -0,0 +1,51 @@
1+use niri::render_helpers::border::BorderRenderElement;
2+use niri_config::{Color, CornerRadius, GradientColorSpace, GradientInterpolation};
3+use smithay::backend::renderer::element::RenderElement;
4+use smithay::backend::renderer::gles::GlesRenderer;
5+use smithay::utils::{Logical, Physical, Rectangle, Size};
6+
7+use super::TestCase;
8+
9+pub struct GradientSrgbLinearAlpha {
10+ gradient_format: GradientInterpolation,
11+}
12+
13+impl GradientSrgbLinearAlpha {
14+ pub fn new(_size: Size<i32, Logical>) -> Self {
15+ Self {
16+ gradient_format: GradientInterpolation {
17+ color_space: GradientColorSpace::SrgbLinear,
18+ hue_interpolation: Default::default(),
19+ },
20+ }
21+ }
22+}
23+
24+impl TestCase for GradientSrgbLinearAlpha {
25+ fn render(
26+ &mut self,
27+ _renderer: &mut GlesRenderer,
28+ size: Size<i32, Physical>,
29+ ) -> Vec<Box<dyn RenderElement<GlesRenderer>>> {
30+ let (a, b) = (size.w / 6, size.h / 3);
31+ let size = (size.w - a * 2, size.h - b * 2);
32+ let area = Rectangle::from_loc_and_size((a, b), size).to_f64();
33+
34+ [BorderRenderElement::new(
35+ area.size,
36+ Rectangle::from_loc_and_size((0., 0.), area.size),
37+ self.gradient_format,
38+ Color::new_unpremul(1., 0., 0., 1.),
39+ Color::new_unpremul(0., 1., 0., 0.),
40+ 0.,
41+ Rectangle::from_loc_and_size((0., 0.), area.size),
42+ 0.,
43+ CornerRadius::default(),
44+ 1.,
45+ )
46+ .with_location(area.loc)]
47+ .into_iter()
48+ .map(|elem| Box::new(elem) as _)
49+ .collect()
50+ }
51+}
niri-visual-tests/src/cases/layout.rsmodified+2 -2
@@ -50,8 +50,8 @@ impl Layout {
5050 border: niri_config::Border {
5151 off: false,
5252 width: FloatOrInt(4.),
53- active_color: Color::new(255, 163, 72, 255),
54- inactive_color: Color::new(50, 50, 50, 255),
53+ active_color: Color::from_rgba8_unpremul(255, 163, 72, 255),
54+ inactive_color: Color::from_rgba8_unpremul(50, 50, 50, 255),
5555 active_gradient: None,
5656 inactive_gradient: None,
5757 },
niri-visual-tests/src/cases/mod.rsmodified+11 -0
@@ -6,6 +6,17 @@ use smithay::utils::{Physical, Size};
66
77 pub mod gradient_angle;
88 pub mod gradient_area;
9+pub mod gradient_oklab;
10+pub mod gradient_oklab_alpha;
11+pub mod gradient_oklch_alpha;
12+pub mod gradient_oklch_decreasing;
13+pub mod gradient_oklch_increasing;
14+pub mod gradient_oklch_longer;
15+pub mod gradient_oklch_shorter;
16+pub mod gradient_srgb;
17+pub mod gradient_srgb_alpha;
18+pub mod gradient_srgblinear;
19+pub mod gradient_srgblinear_alpha;
920 pub mod layout;
1021 pub mod tile;
1122 pub mod window;
niri-visual-tests/src/cases/tile.rsmodified+1 -1
@@ -72,7 +72,7 @@ impl Tile {
7272 border: niri_config::Border {
7373 off: false,
7474 width: FloatOrInt(32.),
75- active_color: Color::new(255, 163, 72, 255),
75+ active_color: Color::from_rgba8_unpremul(255, 163, 72, 255),
7676 ..Default::default()
7777 },
7878 ..Default::default()
niri-visual-tests/src/main.rsmodified+24 -2
@@ -5,8 +5,6 @@ use std::env;
55 use std::sync::atomic::Ordering;
66
77 use adw::prelude::{AdwApplicationWindowExt, NavigationPageExt};
8-use cases::tile::Tile;
9-use cases::window::Window;
108 use gtk::prelude::{
119 AdjustmentExt, ApplicationExt, ApplicationExtManual, BoxExt, GtkWindowExt, WidgetExt,
1210 };
@@ -18,7 +16,20 @@ use tracing_subscriber::EnvFilter;
1816
1917 use crate::cases::gradient_angle::GradientAngle;
2018 use crate::cases::gradient_area::GradientArea;
19+use crate::cases::gradient_oklab::GradientOklab;
20+use crate::cases::gradient_oklab_alpha::GradientOklabAlpha;
21+use crate::cases::gradient_oklch_alpha::GradientOklchAlpha;
22+use crate::cases::gradient_oklch_decreasing::GradientOklchDecreasing;
23+use crate::cases::gradient_oklch_increasing::GradientOklchIncreasing;
24+use crate::cases::gradient_oklch_longer::GradientOklchLonger;
25+use crate::cases::gradient_oklch_shorter::GradientOklchShorter;
26+use crate::cases::gradient_srgb::GradientSrgb;
27+use crate::cases::gradient_srgb_alpha::GradientSrgbAlpha;
28+use crate::cases::gradient_srgblinear::GradientSrgbLinear;
29+use crate::cases::gradient_srgblinear_alpha::GradientSrgbLinearAlpha;
2130 use crate::cases::layout::Layout;
31+use crate::cases::tile::Tile;
32+use crate::cases::window::Window;
2233 use crate::cases::TestCase;
2334
2435 mod cases;
@@ -112,6 +123,17 @@ fn build_ui(app: &adw::Application) {
112123
113124 s.add(GradientAngle::new, "Gradient - Angle");
114125 s.add(GradientArea::new, "Gradient - Area");
126+ s.add(GradientSrgb::new, "Gradient - Srgb");
127+ s.add(GradientSrgbLinear::new, "Gradient - SrgbLinear");
128+ s.add(GradientOklab::new, "Gradient - Oklab");
129+ s.add(GradientOklchShorter::new, "Gradient - Oklch Shorter");
130+ s.add(GradientOklchLonger::new, "Gradient - Oklch Longer");
131+ s.add(GradientOklchIncreasing::new, "Gradient - Oklch Increasing");
132+ s.add(GradientOklchDecreasing::new, "Gradient - Oklch Decreasing");
133+ s.add(GradientSrgbAlpha::new, "Gradient - Srgb Alpha");
134+ s.add(GradientSrgbLinearAlpha::new, "Gradient - SrgbLinear Alpha");
135+ s.add(GradientOklabAlpha::new, "Gradient - Oklab Alpha");
136+ s.add(GradientOklchAlpha::new, "Gradient - Oklch Alpha");
115137
116138 let content_headerbar = adw::HeaderBar::new();
117139
resources/default-config.kdlmodified+1 -0
@@ -153,6 +153,7 @@ layout {
153153 // The angle is the same as in linear-gradient, and is optional,
154154 // defaulting to 180 (top-to-bottom gradient).
155155 // You can use any CSS linear-gradient tool on the web to set these up.
156+ // Changing the color space is also supported, check the wiki for more info.
156157 //
157158 // active-gradient from="#80c8ff" to="#bbddff" angle=45
158159
src/layout/focus_ring.rsmodified+9 -6
@@ -1,7 +1,7 @@
11 use std::iter::zip;
22
33 use arrayvec::ArrayVec;
4-use niri_config::{CornerRadius, Gradient, GradientRelativeTo};
4+use niri_config::{CornerRadius, Gradient, GradientInterpolation, GradientRelativeTo};
55 use smithay::backend::renderer::element::Kind;
66 use smithay::utils::{Logical, Point, Rectangle, Size};
77
@@ -72,7 +72,7 @@ impl FocusRing {
7272 };
7373
7474 for buf in &mut self.buffers {
75- buf.set_color(color.into());
75+ buf.set_color(color.to_array_premul());
7676 }
7777
7878 let radius = radius.fit_to(self.full_size.w as f32, self.full_size.h as f32);
@@ -91,6 +91,7 @@ impl FocusRing {
9191 to: color,
9292 angle: 0,
9393 relative_to: GradientRelativeTo::Window,
94+ in_: GradientInterpolation::default(),
9495 });
9596
9697 let full_rect = Rectangle::from_loc_and_size((-width, -width), self.full_size);
@@ -178,8 +179,9 @@ impl FocusRing {
178179 border.update(
179180 size,
180181 Rectangle::from_loc_and_size(gradient_area.loc - loc, gradient_area.size),
181- gradient.from.into(),
182- gradient.to.into(),
182+ gradient.in_,
183+ gradient.from,
184+ gradient.to,
183185 ((gradient.angle as f32) - 90.).to_radians(),
184186 Rectangle::from_loc_and_size(full_rect.loc - loc, full_rect.size),
185187 rounded_corner_border_width,
@@ -198,8 +200,9 @@ impl FocusRing {
198200 gradient_area.loc - self.locations[0],
199201 gradient_area.size,
200202 ),
201- gradient.from.into(),
202- gradient.to.into(),
203+ gradient.in_,
204+ gradient.from,
205+ gradient.to,
203206 ((gradient.angle as f32) - 90.).to_radians(),
204207 Rectangle::from_loc_and_size(full_rect.loc - self.locations[0], full_rect.size),
205208 rounded_corner_border_width,
src/layout/tile.rsmodified+4 -3
@@ -1,7 +1,7 @@
11 use std::rc::Rc;
22 use std::time::Duration;
33
4-use niri_config::CornerRadius;
4+use niri_config::{Color, CornerRadius, GradientInterpolation};
55 use smithay::backend::allocator::Fourcc;
66 use smithay::backend::renderer::element::{Element, Kind};
77 use smithay::backend::renderer::gles::GlesRenderer;
@@ -757,8 +757,9 @@ impl<W: LayoutElement> Tile<W> {
757757 return BorderRenderElement::new(
758758 geo.size,
759759 Rectangle::from_loc_and_size((0., 0.), geo.size),
760- elem.color(),
761- elem.color(),
760+ GradientInterpolation::default(),
761+ Color::from_array_premul(elem.color()),
762+ Color::from_array_premul(elem.color()),
762763 0.,
763764 Rectangle::from_loc_and_size((0., 0.), geo.size),
764765 0.,
src/render_helpers/border.rsmodified+34 -9
@@ -1,7 +1,9 @@
11 use std::collections::HashMap;
22
33 use glam::{Mat3, Vec2};
4-use niri_config::CornerRadius;
4+use niri_config::{
5+ Color, CornerRadius, GradientColorSpace, GradientInterpolation, HueInterpolation,
6+};
57 use smithay::backend::renderer::element::{Element, Id, Kind, RenderElement, UnderlyingStorage};
68 use smithay::backend::renderer::gles::{GlesError, GlesFrame, GlesRenderer, Uniform};
79 use smithay::backend::renderer::utils::{CommitCounter, DamageSet, OpaqueRegions};
@@ -28,8 +30,9 @@ pub struct BorderRenderElement {
2830 struct Parameters {
2931 size: Size<f64, Logical>,
3032 gradient_area: Rectangle<f64, Logical>,
31- color_from: [f32; 4],
32- color_to: [f32; 4],
33+ gradient_format: GradientInterpolation,
34+ color_from: Color,
35+ color_to: Color,
3336 angle: f32,
3437 geometry: Rectangle<f64, Logical>,
3538 border_width: f32,
@@ -43,8 +46,9 @@ impl BorderRenderElement {
4346 pub fn new(
4447 size: Size<f64, Logical>,
4548 gradient_area: Rectangle<f64, Logical>,
46- color_from: [f32; 4],
47- color_to: [f32; 4],
49+ gradient_format: GradientInterpolation,
50+ color_from: Color,
51+ color_to: Color,
4852 angle: f32,
4953 geometry: Rectangle<f64, Logical>,
5054 border_width: f32,
@@ -57,6 +61,7 @@ impl BorderRenderElement {
5761 params: Parameters {
5862 size,
5963 gradient_area,
64+ gradient_format,
6065 color_from,
6166 color_to,
6267 angle,
@@ -77,6 +82,7 @@ impl BorderRenderElement {
7782 params: Parameters {
7883 size: Default::default(),
7984 gradient_area: Default::default(),
85+ gradient_format: GradientInterpolation::default(),
8086 color_from: Default::default(),
8187 color_to: Default::default(),
8288 angle: 0.,
@@ -97,8 +103,9 @@ impl BorderRenderElement {
97103 &mut self,
98104 size: Size<f64, Logical>,
99105 gradient_area: Rectangle<f64, Logical>,
100- color_from: [f32; 4],
101- color_to: [f32; 4],
106+ gradient_format: GradientInterpolation,
107+ color_from: Color,
108+ color_to: Color,
102109 angle: f32,
103110 geometry: Rectangle<f64, Logical>,
104111 border_width: f32,
@@ -108,6 +115,7 @@ impl BorderRenderElement {
108115 let params = Parameters {
109116 size,
110117 gradient_area,
118+ gradient_format,
111119 color_from,
112120 color_to,
113121 angle,
@@ -128,6 +136,7 @@ impl BorderRenderElement {
128136 let Parameters {
129137 size,
130138 gradient_area,
139+ gradient_format,
131140 color_from,
132141 color_to,
133142 angle,
@@ -162,13 +171,29 @@ impl BorderRenderElement {
162171 let input_to_geo =
163172 Mat3::from_scale(area_size) * Mat3::from_translation(-geo_loc / area_size);
164173
174+ let colorspace = match gradient_format.color_space {
175+ GradientColorSpace::Srgb => 0.,
176+ GradientColorSpace::SrgbLinear => 1.,
177+ GradientColorSpace::Oklab => 2.,
178+ GradientColorSpace::Oklch => 3.,
179+ };
180+
181+ let hue_interpolation = match gradient_format.hue_interpolation {
182+ HueInterpolation::Shorter => 0.,
183+ HueInterpolation::Longer => 1.,
184+ HueInterpolation::Increasing => 2.,
185+ HueInterpolation::Decreasing => 3.,
186+ };
187+
165188 self.inner.update(
166189 size,
167190 None,
168191 scale,
169192 vec![
170- Uniform::new("color_from", color_from),
171- Uniform::new("color_to", color_to),
193+ Uniform::new("colorspace", colorspace),
194+ Uniform::new("hue_interpolation", hue_interpolation),
195+ Uniform::new("color_from", color_from.to_array_unpremul()),
196+ Uniform::new("color_to", color_to.to_array_unpremul()),
172197 Uniform::new("grad_offset", grad_offset.to_array()),
173198 Uniform::new("grad_width", w),
174199 Uniform::new("grad_vec", grad_vec.to_array()),
src/render_helpers/shaders/border.fragmodified+173 -1
@@ -10,6 +10,8 @@ uniform float niri_scale;
1010 uniform vec2 niri_size;
1111 varying vec2 niri_v_coords;
1212
13+uniform float colorspace;
14+uniform float hue_interpolation;
1315 uniform vec4 color_from;
1416 uniform vec4 color_to;
1517 uniform vec2 grad_offset;
@@ -21,6 +23,176 @@ uniform vec2 geo_size;
2123 uniform vec4 outer_radius;
2224 uniform float border_width;
2325
26+vec4 premul_rect(vec4 color) {
27+ color.rgb *= color.a;
28+ return color;
29+}
30+
31+vec4 premul_lch(vec4 color) {
32+ color.xy *= color.a;
33+ return color;
34+}
35+
36+vec4 unpremul_rect(vec4 color) {
37+ if (color.a == 0.0)
38+ return color;
39+
40+ color.rgb /= color.a;
41+ return color;
42+}
43+
44+vec4 unpremul_lch(vec4 color) {
45+ if (color.a == 0.0)
46+ return color;
47+
48+ color.xy /= color.a;
49+ return color;
50+}
51+
52+vec4 premul_mix_unpremul_rect(vec4 color1, vec4 color2, float ratio) {
53+ vec4 mixed = mix(premul_rect(color1), premul_rect(color2), ratio);
54+ return unpremul_rect(mixed);
55+}
56+
57+vec4 premul_mix_unpremul_lch(vec4 color1, vec4 color2, float ratio) {
58+ vec4 mixed = mix(premul_lch(color1), premul_lch(color2), ratio);
59+ return unpremul_lch(mixed);
60+}
61+
62+vec3 srgb_to_linear(vec3 color) {
63+ return pow(color, vec3(2.2));
64+}
65+
66+vec3 linear_to_srgb(vec3 color) {
67+ return pow(color, vec3(1.0 / 2.2));
68+}
69+
70+vec3 lab_to_lch(vec3 color) {
71+ float c = sqrt(pow(color.y, 2.0) + pow(color.z, 2.0));
72+ float h = degrees(atan(color.z, color.y)) ;
73+ h += h <= 0.0 ?
74+ 360.0 :
75+ 0.0 ;
76+ return vec3(
77+ color.x,
78+ c,
79+ h
80+ );
81+}
82+
83+vec3 lch_to_lab(vec3 color) {
84+ float a = color.y * clamp(cos(radians(color.z)), -1.0, 1.0);
85+ float b = color.y * clamp(sin(radians(color.z)), -1.0, 1.0);
86+ return vec3(
87+ color.x,
88+ a,
89+ b
90+ );
91+}
92+
93+vec3 linear_to_oklab(vec3 color){
94+ mat3 rgb_to_lms = mat3(
95+ vec3(0.4122214708, 0.5363325363, 0.0514459929),
96+ vec3(0.2119034982, 0.6806995451, 0.1073969566),
97+ vec3(0.0883024619, 0.2817188376, 0.6299787005)
98+ );
99+ mat3 lms_to_oklab = mat3(
100+ vec3(0.2104542553, 0.7936177850, -0.0040720468),
101+ vec3(1.9779984951, -2.4285922050, 0.4505937099),
102+ vec3(0.0259040371, 0.7827717662, -0.8086757660)
103+ );
104+ vec3 lms = color * rgb_to_lms;
105+ lms = pow(lms, vec3(1.0 / 3.0));
106+ return lms * lms_to_oklab;
107+}
108+
109+vec3 oklab_to_linear(vec3 color){
110+ mat3 oklab_to_lms = mat3(
111+ vec3(1.0, 0.3963377774, 0.2158037573),
112+ vec3(1.0, -0.1055613458, -0.0638541728),
113+ vec3(1.0, -0.0894841775, -1.2914855480)
114+ );
115+ mat3 lms_to_rgb = mat3(
116+ vec3(4.0767416621, -3.3077115913, 0.2309699292),
117+ vec3(-1.2684380046, 2.6097574011, -0.3413193965),
118+ vec3(-0.0041960863, -0.7034186147, 1.7076147010)
119+ );
120+ vec3 lms = color * oklab_to_lms;
121+ lms = pow(lms, vec3(3.0));
122+ return lms * lms_to_rgb;
123+}
124+
125+vec4 color_mix(vec4 color1, vec4 color2, float color_ratio) {
126+ vec4 color_out;
127+
128+ // srgb
129+ if (colorspace == 0.0) {
130+ return mix(premul_rect(color1), premul_rect(color2), color_ratio);
131+ }
132+
133+ color1.rgb = srgb_to_linear(color1.rgb);
134+ color2.rgb = srgb_to_linear(color2.rgb);
135+
136+ // srgb-linear
137+ if (colorspace == 1.0) {
138+ color_out = premul_mix_unpremul_rect(color1, color2, color_ratio);
139+ // oklab
140+ } else if (colorspace == 2.0) {
141+ color1.xyz = linear_to_oklab(color1.rgb);
142+ color2.xyz = linear_to_oklab(color2.rgb);
143+ color_out = premul_mix_unpremul_rect(color1, color2, color_ratio);
144+ color_out.rgb = oklab_to_linear(color_out.xyz);
145+ // oklch
146+ } else if (colorspace == 3.0) {
147+ color1.xyz = lab_to_lch(linear_to_oklab(color1.rgb));
148+ color2.xyz = lab_to_lch(linear_to_oklab(color2.rgb));
149+ color_out = premul_mix_unpremul_lch(color1, color2, color_ratio);
150+
151+ float min_hue = min(color1.z, color2.z);
152+ float max_hue = max(color1.z, color2.z);
153+ float path_direct_distance = (max_hue - min_hue) * color_ratio;
154+ float path_mod_distance = (360.0 - max_hue + min_hue) * color_ratio;
155+
156+ float path_mod =
157+ color1.z == min_hue ?
158+ mod(color1.z - path_mod_distance, 360.0) :
159+ mod(color1.z + path_mod_distance, 360.0) ;
160+ float path_direct =
161+ color1.z == min_hue ?
162+ color1.z + path_direct_distance :
163+ color1.z - path_direct_distance ;
164+
165+ // shorter
166+ if (hue_interpolation == 0.0) {
167+ color_out.z =
168+ max_hue - min_hue > 360.0 - max_hue + min_hue ?
169+ path_mod :
170+ path_direct ;
171+ // longer
172+ } else if (hue_interpolation == 1.0) {
173+ color_out.z =
174+ max_hue - min_hue <= 360.0 - max_hue + min_hue ?
175+ path_mod :
176+ path_direct ;
177+ // increasing
178+ } else if (hue_interpolation == 2.0) {
179+ color_out.z =
180+ color1.z > color2.z ?
181+ path_mod :
182+ path_direct ;
183+ // decreasing
184+ } else if (hue_interpolation == 3.0) {
185+ color_out.z =
186+ color1.z <= color2.z ?
187+ path_mod :
188+ path_direct ;
189+ }
190+ color_out.rgb = clamp(oklab_to_linear(lch_to_lab(color_out.xyz)), 0.0, 1.0);
191+ }
192+
193+ return premul_rect(vec4(linear_to_srgb(color_out.rgb), color_out.a));
194+}
195+
24196 vec4 gradient_color(vec2 coords) {
25197 coords = coords + grad_offset;
26198
@@ -33,7 +205,7 @@ vec4 gradient_color(vec2 coords) {
33205 frac += 1.0;
34206
35207 frac = clamp(frac, 0.0, 1.0);
36- return mix(color_from, color_to, frac);
208+ return color_mix(color_from, color_to, frac);
37209 }
38210
39211 float rounding_alpha(vec2 coords, vec2 size, vec4 corner_radius) {
src/render_helpers/shaders/mod.rsmodified+2 -0
@@ -34,6 +34,8 @@ impl Shaders {
3434 renderer,
3535 include_str!("border.frag"),
3636 &[
37+ UniformName::new("colorspace", UniformType::_1f),
38+ UniformName::new("hue_interpolation", UniformType::_1f),
3739 UniformName::new("color_from", UniformType::_4f),
3840 UniformName::new("color_to", UniformType::_4f),
3941 UniformName::new("grad_offset", UniformType::_2f),
src/window/mapped.rsmodified+4 -3
@@ -2,7 +2,7 @@ use std::cell::{Cell, RefCell};
22 use std::cmp::{max, min};
33 use std::time::Duration;
44
5-use niri_config::{CornerRadius, WindowRule};
5+use niri_config::{Color, CornerRadius, GradientInterpolation, WindowRule};
66 use smithay::backend::renderer::element::surface::render_elements_from_surface_tree;
77 use smithay::backend::renderer::element::{Id, Kind};
88 use smithay::backend::renderer::gles::GlesRenderer;
@@ -289,8 +289,9 @@ impl Mapped {
289289 return BorderRenderElement::new(
290290 geo.size,
291291 Rectangle::from_loc_and_size((0., 0.), geo.size),
292- elem.color(),
293- elem.color(),
292+ GradientInterpolation::default(),
293+ Color::from_array_premul(elem.color()),
294+ Color::from_array_premul(elem.color()),
294295 0.,
295296 Rectangle::from_loc_and_size((0., 0.), geo.size),
296297 0.,
wiki/Configuration:-Layout.mdmodified+23 -2
@@ -32,7 +32,7 @@ layout {
3232 active-color "#ffc87f"
3333 inactive-color "#505050"
3434 // active-gradient from="#ffbb66" to="#ffc880" angle=45 relative-to="workspace-view"
35- // inactive-gradient from="#505050" to="#808080" angle=45 relative-to="workspace-view"
35+ // inactive-gradient from="#505050" to="#808080" angle=45 relative-to="workspace-view" in="srgb-linear"
3636 }
3737
3838 struts {
@@ -169,7 +169,7 @@ layout {
169169 inactive-color "#505050"
170170
171171 // active-gradient from="#ffbb66" to="#ffc880" angle=45 relative-to="workspace-view"
172- // inactive-gradient from="#505050" to="#808080" angle=45 relative-to="workspace-view"
172+ // inactive-gradient from="#505050" to="#808080" angle=45 relative-to="workspace-view" in="srgb-linear"
173173 }
174174 }
175175 ```
@@ -238,6 +238,27 @@ layout {
238238 }
239239 ```
240240
241+<sup>Since: 0.1.8</sup> You can set the gradient interpolation color space using syntax like `in="srgb-linear"` or `in="oklch longer hue"`.
242+Supported color spaces are:
243+
244+- `srgb` (the default),
245+- `srgb-linear`,
246+- `oklab`,
247+- `oklch` with `shorter hue` or `longer hue` or `increasing hue` or `decreasing hue`.
248+
249+They are rendered the same as CSS.
250+For example, `active-gradient from="#f00f" to="#0f05" angle=45 in="oklch longer hue"` will look the same as CSS `linear-gradient(45deg in oklch longer hue, #f00f, #0f05)`.
251+
252+![](./img/gradients-oklch.png)
253+
254+```kdl
255+layout {
256+ border {
257+ active-gradient from="#f00f" to="#0f05" angle=45 in="oklch longer hue"
258+ }
259+}
260+```
261+
241262 ### `struts`
242263
243264 Struts shrink the area occupied by windows, similarly to layer-shell panels.
wiki/img/gradients-oklch.pngadded+3 -0
@@ -0,0 +1,3 @@
1+version https://git-lfs.github.com/spec/v1
2+oid sha256:a3e4db583ac849ec3bd6c2312a84a16eaad810ebb7599cad3762a7dfc66e865b
3+size 23180