From 762412243657882a38da6ad991bd0ed0ff3c8de7 Mon Sep 17 00:00:00 2001 From: Bruce Mitchener Date: Tue, 10 Dec 2024 10:50:35 +0700 Subject: [PATCH] Default color types to opaque white We choose an opaque color because otherwise the `OpaqueColor` would have a substantially different default value than the other color types. White is the same as the default color within `bevy_color`. The default color is used by the Brush in Parley and likely other places. --- CHANGELOG.md | 4 +++- color/src/color.rs | 29 +++++++++++++++++++++++++++++ color/src/dynamic.rs | 14 ++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c84791c..839b135 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ This release has an [MSRV][] of 1.82. * `AlphaColor`, `OpaqueColor`, and `PremulColor` now impl `PartialEq`. ([#76][] by [@waywardmonkeys][]) * `HueDirection` now impls `PartialEq`. ([#79][] by [@waywardmonkeys][]) * `ColorSpaceTag` and `HueDirection` now have bytemuck support. ([#81][] by [@waywardmonkeys][]) +* `AlphaColor`, `DynamicColor`, `OpaqueColor`, and `PremulColor` now default to an opaque white. ([#85][] by [@waywardmonkeys][]) ### Changed @@ -61,7 +62,8 @@ This is the initial release. [#78]: https://github.com/linebender/color/pull/78 [#79]: https://github.com/linebender/color/pull/79 [#80]: https://github.com/linebender/color/pull/80 -[#81]: https://github.com/linebender/color/pull/80 +[#81]: https://github.com/linebender/color/pull/81 +[#85]: https://github.com/linebender/color/pull/85 [Unreleased]: https://github.com/linebender/color/compare/v0.1.0...HEAD [0.1.0]: https://github.com/linebender/color/releases/tag/v0.1.0 diff --git a/color/src/color.rs b/color/src/color.rs index 3d9f58a..f02ecb6 100644 --- a/color/src/color.rs +++ b/color/src/color.rs @@ -21,6 +21,9 @@ use crate::floatfuncs::FloatFuncs; /// major motivation for including these is to enable weighted sums, including /// for spline interpolation. For cylindrical color spaces, hue fixup should /// be applied before interpolation. +/// +/// The default value is an opaque white. We don't recommend relying upon this +/// default. #[derive(Clone, Copy, Debug, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] #[repr(transparent)] @@ -37,6 +40,9 @@ pub struct OpaqueColor { /// /// A color in a color space known at compile time, with an alpha channel. /// +/// The default value is an opaque white. We don't recommend relying upon this +/// default. +/// /// See [`OpaqueColor`] for a discussion of arithmetic traits and interpolation. #[derive(Clone, Copy, Debug, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] @@ -60,6 +66,9 @@ pub struct AlphaColor { /// the hue channel is not premultiplied. If it were, interpolation would /// give undesirable results. /// +/// The default value is an opaque white. We don't recommend relying upon this +/// default. +/// /// See [`OpaqueColor`] for a discussion of arithmetic traits and interpolation. #[derive(Clone, Copy, Debug, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] @@ -637,6 +646,26 @@ impl PremulColor { } } +// Defaults + +impl Default for AlphaColor { + fn default() -> Self { + Self::WHITE + } +} + +impl Default for OpaqueColor { + fn default() -> Self { + Self::WHITE + } +} + +impl Default for PremulColor { + fn default() -> Self { + Self::WHITE + } +} + // Lossless conversion traits. impl From> for AlphaColor { diff --git a/color/src/dynamic.rs b/color/src/dynamic.rs index 69389f7..c5ef4d0 100644 --- a/color/src/dynamic.rs +++ b/color/src/dynamic.rs @@ -6,6 +6,7 @@ use crate::{ color::{add_alpha, fixup_hues_for_interpolate, split_alpha}, AlphaColor, ColorSpace, ColorSpaceLayout, ColorSpaceTag, HueDirection, LinearSrgb, Missing, + Srgb, }; use core::hash::{Hash, Hasher}; @@ -25,6 +26,9 @@ use core::hash::{Hash, Hasher}; /// When manipulating components directly, setting them nonzero when the /// corresponding missing flag is set may yield unexpected results. /// +/// The default value is an opaque white in the [sRGB](Srgb) colorspace with +/// no missing components. We don't recommend relying upon this default. +/// /// [Oklch]: crate::Oklch #[derive(Clone, Copy, Debug)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] @@ -365,6 +369,16 @@ impl DynamicColor { } } +impl Default for DynamicColor { + fn default() -> Self { + Self { + cs: ColorSpaceTag::Srgb, + missing: Missing::default(), + components: add_alpha(Srgb::WHITE_COMPONENTS, 1.), + } + } +} + impl Hash for DynamicColor { /// The hash is computed from the bit representation of the component values. /// That makes it suitable for use as a cache key or memoization, but does not