From 68b3ce274a1f02b8cc19020d4e6d7e9725e5a452 Mon Sep 17 00:00:00 2001 From: Bruce Mitchener Date: Mon, 2 Dec 2024 08:17:30 +0700 Subject: [PATCH] Remove `ColorF64` (#759) This is a step towards using `color` for the gradients, first moving to use the `lerp` function in `AlphaColor` and getting rid of `ColorF64` since our colors are now `f32`-based. --- vello_encoding/src/ramp_cache.rs | 47 ++++------------------- vello_tests/snapshots/big_colr.png | 4 +- vello_tests/snapshots/gradient_extend.png | 4 +- vello_tests/snapshots/little_colr.png | 4 +- 4 files changed, 14 insertions(+), 45 deletions(-) diff --git a/vello_encoding/src/ramp_cache.rs b/vello_encoding/src/ramp_cache.rs index 84ad5677d..4ddd423fa 100644 --- a/vello_encoding/src/ramp_cache.rs +++ b/vello_encoding/src/ramp_cache.rs @@ -3,8 +3,8 @@ use std::collections::HashMap; -use peniko::color::Srgb; -use peniko::{Color, ColorStop, ColorStops}; +use peniko::color::{HueDirection, Srgb}; +use peniko::{ColorStop, ColorStops}; const N_SAMPLES: usize = 512; const RETAINED_COUNT: usize = 64; @@ -82,18 +82,18 @@ impl RampCache { fn make_ramp(stops: &[ColorStop]) -> impl Iterator + '_ { let mut last_u = 0.0; - let mut last_c = ColorF64::from_color(stops[0].color.to_alpha_color::()); + let mut last_c = stops[0].color.to_alpha_color::(); let mut this_u = last_u; let mut this_c = last_c; let mut j = 0; (0..N_SAMPLES).map(move |i| { - let u = (i as f64) / (N_SAMPLES - 1) as f64; + let u = (i as f32) / (N_SAMPLES - 1) as f32; while u > this_u { last_u = this_u; last_c = this_c; if let Some(s) = stops.get(j + 1) { - this_u = s.offset as f64; - this_c = ColorF64::from_color(s.color.to_alpha_color::()); + this_u = s.offset; + this_c = s.color.to_alpha_color::(); j += 1; } else { break; @@ -103,39 +103,8 @@ fn make_ramp(stops: &[ColorStop]) -> impl Iterator + '_ { let c = if du < 1e-9 { this_c } else { - last_c.lerp(&this_c, (u - last_u) / du) + last_c.lerp(this_c, (u - last_u) / du, HueDirection::default()) }; - c.as_premul_u32() + c.premultiply().to_rgba8().to_u32() }) } - -#[derive(Copy, Clone, Debug)] -struct ColorF64([f64; 4]); - -impl ColorF64 { - fn from_color(color: Color) -> Self { - let [r, g, b, a] = color.components; - Self([r as f64, g as f64, b as f64, a as f64]) - } - - fn lerp(&self, other: &Self, a: f64) -> Self { - fn l(x: f64, y: f64, a: f64) -> f64 { - x * (1.0 - a) + y * a - } - Self([ - l(self.0[0], other.0[0], a), - l(self.0[1], other.0[1], a), - l(self.0[2], other.0[2], a), - l(self.0[3], other.0[3], a), - ]) - } - - fn as_premul_u32(&self) -> u32 { - let a = self.0[3].clamp(0.0, 1.0); - let r = ((self.0[0] * a).clamp(0.0, 1.0) * 255.0) as u32; - let g = ((self.0[1] * a).clamp(0.0, 1.0) * 255.0) as u32; - let b = ((self.0[2] * a).clamp(0.0, 1.0) * 255.0) as u32; - let a = (a * 255.0) as u32; - r | (g << 8) | (b << 16) | (a << 24) - } -} diff --git a/vello_tests/snapshots/big_colr.png b/vello_tests/snapshots/big_colr.png index 40386f9de..c7bc77824 100644 --- a/vello_tests/snapshots/big_colr.png +++ b/vello_tests/snapshots/big_colr.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:f10d642ad14b33815998273a8a44fb6ddb1d7b8e161b550d498200a584f8370f -size 15853 +oid sha256:c43e1738715b4b4359bdda7831f4bedbac3db182884c78bdcabd44fc49fffd55 +size 15735 diff --git a/vello_tests/snapshots/gradient_extend.png b/vello_tests/snapshots/gradient_extend.png index a0b738b10..f1cf71a9e 100644 --- a/vello_tests/snapshots/gradient_extend.png +++ b/vello_tests/snapshots/gradient_extend.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:b39d3054c979222c4030f76ed7045f5f82499a327e8a9e00af411d1b50b75b33 -size 55386 +oid sha256:b6a055128e09745f2ceedd753431f32602c2c5618bd8a563123ae7670a2f5932 +size 55394 diff --git a/vello_tests/snapshots/little_colr.png b/vello_tests/snapshots/little_colr.png index 64a112292..69796d060 100644 --- a/vello_tests/snapshots/little_colr.png +++ b/vello_tests/snapshots/little_colr.png @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:4aabea4b8cbf754fdf29c9d454f0880bfb9b0d380818d2ddb1d9128aa80fddb9 -size 2039 +oid sha256:842fa191cb557818fc3ff5663801d297c22d71eb0eacafbd757a7cadf6aae1c7 +size 1938