| 1 | //! Types for the CIE CAM16 color appearance model. |
| 2 | //! |
| 3 | //! CIE CAM16 is a color appearance model that tries to predict the appearance |
| 4 | //! of a color under certain viewing conditions, as specified via the |
| 5 | //! [`Parameters`] type. The [`Cam16`] type has descriptions for the CAM16 |
| 6 | //! attributes. The [Color appearance model page on Wikipedia][wikipedia_cam] |
| 7 | //! has some history and background as well. |
| 8 | //! |
| 9 | //! # Converting Between XYZ and CAM16 |
| 10 | //! |
| 11 | //! The CIE CAM16 implementation in Palette has the [`Cam16`] type and its |
| 12 | //! partial variants on one side of the boundary, and [`Xyz`](crate::Xyz) on the |
| 13 | //! other. Going between `Xyz` and `Cam16` requires the viewing conditions to be |
| 14 | //! specified as [`Parameters`]. |
| 15 | //! |
| 16 | //! ``` |
| 17 | //! use palette::{ |
| 18 | //! Srgb, FromColor, IntoColor, |
| 19 | //! cam16::{Cam16, Parameters}, |
| 20 | //! }; |
| 21 | //! |
| 22 | //! // Customize these according to the viewing conditions: |
| 23 | //! let mut example_parameters = Parameters::default_static_wp(40.0); |
| 24 | //! |
| 25 | //! // CAM16 from sRGB, or most other color spaces: |
| 26 | //! let rgb = Srgb::new(0.3f32, 0.8, 0.1); |
| 27 | //! let cam16_from_rgb = Cam16::from_xyz(rgb.into_color(), example_parameters); |
| 28 | //! |
| 29 | //! // sRGB from CAM16, using lightness, chroma and hue by default: |
| 30 | //! let rgb_from_cam16 = Srgb::from_color(cam16_from_rgb.into_xyz(example_parameters)); |
| 31 | //! ``` |
| 32 | //! |
| 33 | //! For more control over the attributes to use when converting from CAM16, one |
| 34 | //! of the partial CAM16 types can be used: |
| 35 | //! |
| 36 | //! * [`Cam16Jch`]: lightness and chroma. |
| 37 | //! * [`Cam16Jmh`]: lightness and colorfulness. |
| 38 | //! * [`Cam16Jsh`]: lightness and saturation. |
| 39 | //! * [`Cam16Qch`]: brightness and chroma. |
| 40 | //! * [`Cam16Qmh`]: brightness and colorfulness. |
| 41 | //! * [`Cam16Qsh`]: brightness and saturation. |
| 42 | //! |
| 43 | //! Generic traits and functions can make use of the [`IntoCam16Unclamped`], |
| 44 | //! [`FromCam16Unclamped`], [`Cam16IntoUnclamped`], and [`Cam16FromUnclamped`] |
| 45 | //! traits. They are similar to the traits from the [`convert`][crate::convert] |
| 46 | //! module and help abstracting away most of the implementation details. |
| 47 | //! |
| 48 | //! # The CAM16-UCS Color Space |
| 49 | //! |
| 50 | //! CIE CAM16 specifies a visually uniform color space that can be used for |
| 51 | //! color manipulation. It's represented by the [`Cam16UcsJmh`] and |
| 52 | //! [`Cam16UcsJab`] types, similar to [`Lch`][crate::Lch] and |
| 53 | //! [`Lab`][crate::Lab]. |
| 54 | //! |
| 55 | //! ``` |
| 56 | //! use palette::{ |
| 57 | //! Srgb, FromColor, IntoColor, |
| 58 | //! cam16::{Cam16Jmh, Parameters, Cam16UcsJmh}, |
| 59 | //! }; |
| 60 | //! |
| 61 | //! // Customize these according to the viewing conditions: |
| 62 | //! let mut example_parameters = Parameters::default_static_wp(40.0); |
| 63 | //! |
| 64 | //! // CAM16-UCS from sRGB, or most other color spaces: |
| 65 | //! let rgb = Srgb::new(0.3f32, 0.8, 0.1); |
| 66 | //! let cam16 = Cam16Jmh::from_xyz(rgb.into_color(), example_parameters); |
| 67 | //! let mut ucs_from_rgb = Cam16UcsJmh::from_color(cam16); |
| 68 | //! |
| 69 | //! // Shift the hue by 120 degrees in CAM16-UCS: |
| 70 | //! ucs_from_rgb.hue += 120.0; |
| 71 | //! |
| 72 | //! // Convert back to sRGB under the same viewing conditions: |
| 73 | //! let rgb = Srgb::from_color(Cam16Jmh::from_color(ucs_from_rgb).into_xyz(example_parameters)); |
| 74 | //! ``` |
| 75 | //! |
| 76 | //! [wikipedia_cam]: https://en.wikipedia.org/wiki/Color_appearance_model |
| 77 | |
| 78 | pub use fullrust-analyzer cargo palette 0.7.6 cam16/full/::*; |
| 79 | pub use parametersrust-analyzer cargo palette 0.7.6 cam16/parameters/::*; |
| 80 | pub use partialrust-analyzer cargo palette 0.7.6 cam16/partial/::*; |
| 81 | pub use ucs_jabrust-analyzer cargo palette 0.7.6 cam16/ucs_jab/::{Cam16UcsJabrust-analyzer cargo palette 0.7.6 cam16/ucs_jab/Cam16UcsJab#, Cam16UcsJabarust-analyzer cargo palette 0.7.6 cam16/ucs_jab/Cam16UcsJaba#, Iterrust-analyzer cargo palette 0.7.6 cam16/ucs_jab/Iter# as Cam16UcsJabIterrust-analyzer cargo palette 0.7.6 cam16/ucs_jab/Iter#}; |
| 82 | pub use ucs_jmhrust-analyzer cargo palette 0.7.6 cam16/ucs_jmh/::{Cam16UcsJmhrust-analyzer cargo palette 0.7.6 cam16/ucs_jmh/Cam16UcsJmh#, Cam16UcsJmharust-analyzer cargo palette 0.7.6 cam16/ucs_jmh/Cam16UcsJmha#, Iterrust-analyzer cargo palette 0.7.6 cam16/ucs_jmh/Iter# as Cam16UcsJmhIterrust-analyzer cargo palette 0.7.6 cam16/ucs_jmh/Iter#}; |
| 83 | |
| 84 | #[cfg(feature = "random")] |
| 85 | pub use ucs_jabrust-analyzer cargo palette 0.7.6 cam16/ucs_jab/::UniformCam16UcsJabrust-analyzer cargo palette 0.7.6 cam16/ucs_jab/UniformCam16UcsJab#; |
| 86 | #[cfg(feature = "random")] |
| 87 | pub use ucs_jmhrust-analyzer cargo palette 0.7.6 cam16/ucs_jmh/::UniformCam16UcsJmhrust-analyzer cargo palette 0.7.6 cam16/ucs_jmh/UniformCam16UcsJmh#; |
| 88 | |
| 89 | mod fullrust-analyzer cargo palette 0.7.6 cam16/full/; |
| 90 | pub(craterust-analyzer cargo palette 0.7.6 crate/) mod mathrust-analyzer cargo palette 0.7.6 cam16/math/; |
| 91 | mod parametersrust-analyzer cargo palette 0.7.6 cam16/parameters/; |
| 92 | mod partialrust-analyzer cargo palette 0.7.6 cam16/partial/; |
| 93 | mod ucs_jabrust-analyzer cargo palette 0.7.6 cam16/ucs_jab/; |
| 94 | mod ucs_jmhrust-analyzer cargo palette 0.7.6 cam16/ucs_jmh/; |
| 95 | |
| 96 | /// A trait for converting into a CAM16 color type from `C` without clamping. |
| 97 | pub trait Cam16FromUnclampedrust-analyzer cargo palette 0.7.6 cam16/Cam16FromUnclamped#<WpParam, C> { |
| 98 | /// The number type that's used in `parameters` when converting. |
| 99 | type Scalarrust-analyzer cargo palette 0.7.6 cam16/Cam16FromUnclamped#Scalar#; |
| 100 | |
| 101 | /// Converts `color` into `Self`, using the provided parameters. |
| 102 | fn cam16_from_unclampedrust-analyzer cargo palette 0.7.6 cam16/Cam16FromUnclamped#cam16_from_unclamped().(color: C, parameters: BakedParametersrust-analyzer cargo palette 0.7.6 cam16/parameters/BakedParameters#<WpParam, Self::Scalarrust-analyzer cargo palette 0.7.6 cam16/Cam16FromUnclamped#Scalar#>) -> Self; |
| 103 | } |
| 104 | |
| 105 | /// A trait for converting into a CAM16 color type `C` without clamping. |
| 106 | pub trait IntoCam16Unclampedrust-analyzer cargo palette 0.7.6 cam16/IntoCam16Unclamped#<WpParam, C> { |
| 107 | /// The number type that's used in `parameters` when converting. |
| 108 | type Scalarrust-analyzer cargo palette 0.7.6 cam16/IntoCam16Unclamped#Scalar#; |
| 109 | |
| 110 | /// Converts `self` into `C`, using the provided parameters. |
| 111 | fn into_cam16_unclampedrust-analyzer cargo palette 0.7.6 cam16/IntoCam16Unclamped#into_cam16_unclamped().(self, parameters: BakedParametersrust-analyzer cargo palette 0.7.6 cam16/parameters/BakedParameters#<WpParam, Self::Scalarrust-analyzer cargo palette 0.7.6 cam16/IntoCam16Unclamped#Scalar#>) -> C; |
| 112 | } |
| 113 | |
| 114 | impl<WpParam, T, U> IntoCam16Unclampedrust-analyzer cargo palette 0.7.6 cam16/IntoCam16Unclamped#<WpParam, T> for U |
| 115 | where |
| 116 | T: Cam16FromUnclampedrust-analyzer cargo palette 0.7.6 cam16/Cam16FromUnclamped#<WpParam, U>, |
| 117 | { |
| 118 | type Scalarrust-analyzer cargo palette 0.7.6 cam16/impl#[U][`IntoCam16Unclamped<WpParam, T>`]Scalar# = T::Scalarrust-analyzer cargo palette 0.7.6 cam16/Cam16FromUnclamped#Scalar#; |
| 119 | |
| 120 | fn into_cam16_unclampedrust-analyzer cargo palette 0.7.6 cam16/impl#[U][`IntoCam16Unclamped<WpParam, T>`]into_cam16_unclamped().(self, parameters: BakedParametersrust-analyzer cargo palette 0.7.6 cam16/parameters/BakedParameters#<WpParam, Selfrust-analyzer cargo palette 0.7.6 cam16/impl#[U][`IntoCam16Unclamped<WpParam, T>`]::Scalarrust-analyzer cargo palette 0.7.6 cam16/IntoCam16Unclamped#Scalar#>) -> T { |
| 121 | T::cam16_from_unclampedrust-analyzer cargo palette 0.7.6 cam16/Cam16FromUnclamped#cam16_from_unclamped().(self, parameters) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /// A trait for converting from a CAM16 color type `C` without clamping. |
| 126 | pub trait FromCam16Unclampedrust-analyzer cargo palette 0.7.6 cam16/FromCam16Unclamped#<WpParam, C> { |
| 127 | /// The number type that's used in `parameters` when converting. |
| 128 | type Scalarrust-analyzer cargo palette 0.7.6 cam16/FromCam16Unclamped#Scalar#; |
| 129 | |
| 130 | /// Converts `cam16` into `Self`, using the provided parameters. |
| 131 | fn from_cam16_unclampedrust-analyzer cargo palette 0.7.6 cam16/FromCam16Unclamped#from_cam16_unclamped().(cam16: C, parameters: BakedParametersrust-analyzer cargo palette 0.7.6 cam16/parameters/BakedParameters#<WpParam, Self::Scalarrust-analyzer cargo palette 0.7.6 cam16/FromCam16Unclamped#Scalar#>) -> Self; |
| 132 | } |
| 133 | |
| 134 | /// A trait for converting from a CAM16 color type into `C` without clamping. |
| 135 | pub trait Cam16IntoUnclampedrust-analyzer cargo palette 0.7.6 cam16/Cam16IntoUnclamped#<WpParam, C> { |
| 136 | /// The number type that's used in `parameters` when converting. |
| 137 | type Scalarrust-analyzer cargo palette 0.7.6 cam16/Cam16IntoUnclamped#Scalar#; |
| 138 | |
| 139 | /// Converts `self` into `C`, using the provided parameters. |
| 140 | fn cam16_into_unclampedrust-analyzer cargo palette 0.7.6 cam16/Cam16IntoUnclamped#cam16_into_unclamped().(self, parameters: BakedParametersrust-analyzer cargo palette 0.7.6 cam16/parameters/BakedParameters#<WpParam, Self::Scalarrust-analyzer cargo palette 0.7.6 cam16/Cam16IntoUnclamped#Scalar#>) -> C; |
| 141 | } |
| 142 | |
| 143 | impl<WpParam, T, U> Cam16IntoUnclampedrust-analyzer cargo palette 0.7.6 cam16/Cam16IntoUnclamped#<WpParam, T> for U |
| 144 | where |
| 145 | T: FromCam16Unclampedrust-analyzer cargo palette 0.7.6 cam16/FromCam16Unclamped#<WpParam, U>, |
| 146 | { |
| 147 | type Scalarrust-analyzer cargo palette 0.7.6 cam16/impl#[U][`Cam16IntoUnclamped<WpParam, T>`]Scalar# = T::Scalarrust-analyzer cargo palette 0.7.6 cam16/FromCam16Unclamped#Scalar#; |
| 148 | |
| 149 | fn cam16_into_unclampedrust-analyzer cargo palette 0.7.6 cam16/impl#[U][`Cam16IntoUnclamped<WpParam, T>`]cam16_into_unclamped().(self, parameters: BakedParametersrust-analyzer cargo palette 0.7.6 cam16/parameters/BakedParameters#<WpParam, Selfrust-analyzer cargo palette 0.7.6 cam16/impl#[U][`Cam16IntoUnclamped<WpParam, T>`]::Scalarrust-analyzer cargo palette 0.7.6 cam16/Cam16IntoUnclamped#Scalar#>) -> T { |
| 150 | T::from_cam16_unclampedrust-analyzer cargo palette 0.7.6 cam16/FromCam16Unclamped#from_cam16_unclamped().(self, parameters) |
| 151 | } |
| 152 | } |