Ogeon / palette

A Rust library for linear color calculations and conversion
Apache License 2.0
748 stars 60 forks source link

Add traits for casting collections of colors to and from other data types #338

Closed Ogeon closed 1 year ago

Ogeon commented 1 year ago

These traits are alternatives to the free functions in cast. The idea is to have less "type information" in the naming and only focus on the high level transform. In essence what From and Into also does. For example, with these changes:

let raw = &[0xFF7F0080u32, 0xFF60BBCC];

// Before
use palette::{rgb::PackedArgb, cast};
let colors = cast::from_uint_slice::<PackedArgb>(raw);

// After
use palette::{rgb::PackedArgb, cast::UintsInto};
let colors: &[PackedArgb] = raw.uints_into();

Casting from owned to slice is limited to a fixed set of owning types, since it's hard to cover everything that resembles a slice. Arrays and slices don't implement Deref<Target=[T]>, for example, while AsRef<[T]> doesn't work for the Self type...

In addition to that, I also upgraded some actions in the workflows and added Miri checks to CI. It's good to keep an eye on those transmutes and pointer casts and even better to do it automatically. I have only been running it manually so far.

github-actions[bot] commented 1 year ago

Benchmark for 3ae99ac

Click to view benchmark | Test | Base | PR | % | |------|--------------|------------------|---| | Cie family/lab to lch | 4.0±0.02µs | 4.0±0.04µs | 0.00% | | Cie family/lab to xyz | 929.3±2.48ns | **925.6±1.99ns** | **-0.40%** | | Cie family/lch to lab | 1445.5±9.88ns | 1449.2±12.10ns | +0.26% | | Cie family/linsrgb to xyz | 366.5±1.35ns | **364.2±2.65ns** | **-0.63%** | | Cie family/linsrgb to xyz - wide::f32x8 | 301.8±0.85ns | **299.6±3.13ns** | **-0.73%** | | Cie family/xyz to lab | **10.2±0.07µs** | 10.3±0.06µs | **+0.98%** | | Cie family/xyz to yxy | 824.3±3.55ns | **820.1±5.32ns** | **-0.51%** | | Cie family/yxy to xyz | 722.1±10.77ns | **714.8±6.26ns** | **-1.01%** | | Matrix functions/matrix_inverse | 15.4±0.07ns | **14.8±0.09ns** | **-3.90%** | | Matrix functions/multiply_3x3 | 11.7±0.05ns | **11.5±0.06ns** | **-1.71%** | | Matrix functions/multiply_rgb_to_xyz | 8.8±0.05ns | 8.8±0.05ns | 0.00% | | Matrix functions/multiply_xyz | 7.2±0.05ns | 7.2±0.05ns | 0.00% | | Matrix functions/multiply_xyz_to_rgb | 7.2±0.05ns | 7.2±0.06ns | 0.00% | | Matrix functions/rgb_to_xyz_matrix | 31.0±0.22ns | 31.1±0.17ns | +0.32% | | Rgb family/hsl to hsv | **861.7±5.32ns** | 870.6±2.07ns | **+1.03%** | | Rgb family/hsl to linear hsl | 9.0±0.03µs | 9.0±0.03µs | 0.00% | | Rgb family/hsl to rgb | **2.8±0.01µs** | 2.9±0.00µs | **+3.57%** | | Rgb family/hsv to hsl | **1183.9±4.55ns** | 1191.8±2.07ns | **+0.67%** | | Rgb family/hsv to hwb | 277.6±4.05ns | 278.9±4.05ns | +0.47% | | Rgb family/hsv to linear hsv | 8.4±0.02µs | 8.4±0.02µs | 0.00% | | Rgb family/hsv to rgb | 2.5±0.02µs | 2.5±0.04µs | 0.00% | | Rgb family/hwb to hsv | **720.9±3.18ns** | 723.7±1.70ns | **+0.39%** | | Rgb family/hwb to linear hwb | 9.9±0.05µs | 9.9±0.05µs | 0.00% | | Rgb family/linear hsl to hsl | 9.8±0.02µs | 9.8±0.01µs | 0.00% | | Rgb family/linear hsv to hsv | 9.3±0.03µs | 9.3±0.03µs | 0.00% | | Rgb family/linear hwb to hwb | 10.5±0.03µs | 10.5±0.03µs | 0.00% | | Rgb family/linsrgb to rgb | 4.5±0.01µs | 4.5±0.01µs | 0.00% | | Rgb family/linsrgb to rgb - wide::f32x8 | 6.1±0.01µs | 6.1±0.01µs | 0.00% | | Rgb family/linsrgb_f32 to rgb_u8 | 1133.1±1.78ns | 1134.6±5.27ns | +0.13% | | Rgb family/linsrgb_f64 to rgb_u8 | 1457.5±3.50ns | 1456.8±4.81ns | -0.05% | | Rgb family/rgb to hsl | **755.6±3.67ns** | 762.2±3.49ns | **+0.87%** | | Rgb family/rgb to hsl - wide::f32x8 | **544.9±3.27ns** | 571.6±1.69ns | **+4.90%** | | Rgb family/rgb to hsv | 703.3±2.50ns | 704.5±1.80ns | +0.17% | | Rgb family/rgb to hsv - wide::f32x8 | **497.3±2.13ns** | 519.3±1.89ns | **+4.42%** | | Rgb family/rgb to linsrgb | 4.6±0.01µs | 4.6±0.02µs | 0.00% | | Rgb family/rgb to linsrgb - wide::f32x8 | 3.7±0.02µs | 3.7±0.04µs | 0.00% | | Rgb family/rgb_u8 to linsrgb_f32 | **365.0±1.72ns** | 367.2±1.54ns | **+0.60%** | | Rgb family/rgb_u8 to linsrgb_f64 | 372.9±1.43ns | 371.6±1.82ns | -0.35% | | Rgb family/xyz to linsrgb | 434.4±0.72ns | **422.9±0.46ns** | **-2.65%** | | Rgb family/xyz to linsrgb - wide::f32x8 | 298.4±0.62ns | 298.6±0.63ns | +0.07% |
Ogeon commented 1 year ago

I replaced the generic implementations with specific, but more consistent implementations. Also added doc examples for each trait.

github-actions[bot] commented 1 year ago

Benchmark for cff7201

Click to view benchmark | Test | Base | PR | % | |------|--------------|------------------|---| | Cie family/lab to lch | **3.1±0.01µs** | 3.2±0.37µs | **+3.23%** | | Cie family/lab to xyz | **772.2±1.97ns** | 773.6±0.46ns | **+0.18%** | | Cie family/lch to lab | 1143.9±0.76ns | 1144.6±4.13ns | +0.06% | | Cie family/linsrgb to xyz | 298.2±0.15ns | **297.0±0.50ns** | **-0.40%** | | Cie family/linsrgb to xyz - wide::f32x8 | 312.6±0.46ns | 312.6±0.65ns | 0.00% | | Cie family/xyz to lab | 8.3±0.02µs | 8.3±0.05µs | 0.00% | | Cie family/xyz to yxy | **656.9±0.31ns** | 659.1±7.97ns | **+0.33%** | | Cie family/yxy to xyz | 611.0±0.68ns | 610.8±0.22ns | -0.03% | | Matrix functions/matrix_inverse | 14.3±0.06ns | 14.3±0.01ns | 0.00% | | Matrix functions/multiply_3x3 | 10.4±0.01ns | 10.4±0.00ns | 0.00% | | Matrix functions/multiply_rgb_to_xyz | 10.0±0.00ns | 10.0±0.00ns | 0.00% | | Matrix functions/multiply_xyz | 6.4±0.00ns | 6.4±0.01ns | 0.00% | | Matrix functions/multiply_xyz_to_rgb | 6.4±0.00ns | 6.4±0.01ns | 0.00% | | Matrix functions/rgb_to_xyz_matrix | 29.1±0.03ns | 29.1±0.03ns | 0.00% | | Rgb family/hsl to hsv | **689.1±0.62ns** | 692.4±0.26ns | **+0.48%** | | Rgb family/hsl to linear hsl | 7.6±0.03µs | 7.6±0.04µs | 0.00% | | Rgb family/hsl to rgb | 2.1±0.00µs | 2.1±0.00µs | 0.00% | | Rgb family/hsv to hsl | 955.4±2.86ns | **951.4±0.67ns** | **-0.42%** | | Rgb family/hsv to hwb | **223.4±0.10ns** | 225.8±0.09ns | **+1.07%** | | Rgb family/hsv to linear hsv | 7.0±0.00µs | 7.0±0.00µs | 0.00% | | Rgb family/hsv to rgb | 2.0±0.00µs | 2.0±0.00µs | 0.00% | | Rgb family/hwb to hsv | 558.8±3.35ns | 557.8±2.82ns | -0.18% | | Rgb family/hwb to linear hwb | 8.2±0.01µs | 8.2±0.01µs | 0.00% | | Rgb family/linear hsl to hsl | 7.6±0.01µs | 7.6±0.00µs | 0.00% | | Rgb family/linear hsv to hsv | 7.1±0.00µs | 7.1±0.00µs | 0.00% | | Rgb family/linear hwb to hwb | 8.2±0.01µs | 8.2±0.01µs | 0.00% | | Rgb family/linsrgb to rgb | 3.8±0.00µs | 3.8±0.00µs | 0.00% | | Rgb family/linsrgb to rgb - wide::f32x8 | 5.5±0.00µs | 5.5±0.00µs | 0.00% | | Rgb family/linsrgb_f32 to rgb_u8 | 1318.8±1.37ns | 1319.4±0.87ns | +0.05% | | Rgb family/linsrgb_f64 to rgb_u8 | **1365.2±0.76ns** | 1375.5±3.52ns | **+0.75%** | | Rgb family/rgb to hsl | 660.7±0.42ns | **656.4±0.51ns** | **-0.65%** | | Rgb family/rgb to hsl - wide::f32x8 | 488.9±0.53ns | **488.3±0.50ns** | **-0.12%** | | Rgb family/rgb to hsv | 485.6±0.27ns | **477.7±0.16ns** | **-1.63%** | | Rgb family/rgb to hsv - wide::f32x8 | 429.3±0.51ns | **428.9±0.37ns** | **-0.09%** | | Rgb family/rgb to linsrgb | 3.8±0.01µs | 3.8±0.00µs | 0.00% | | Rgb family/rgb to linsrgb - wide::f32x8 | 3.6±0.00µs | 3.6±0.00µs | 0.00% | | Rgb family/rgb_u8 to linsrgb_f32 | **328.2±0.47ns** | 334.8±0.10ns | **+2.01%** | | Rgb family/rgb_u8 to linsrgb_f64 | **328.7±0.28ns** | 340.3±0.12ns | **+3.53%** | | Rgb family/xyz to linsrgb | **328.3±1.51ns** | 340.5±0.15ns | **+3.72%** | | Rgb family/xyz to linsrgb - wide::f32x8 | 306.8±0.18ns | **306.6±0.12ns** | **-0.07%** |
Ogeon commented 1 year ago

bors r+

bors[bot] commented 1 year ago

Build succeeded!

The publicly hosted instance of bors-ng is deprecated and will go away soon.

If you want to self-host your own instance, instructions are here. For more help, visit the forum.

If you want to switch to GitHub's built-in merge queue, visit their help page.