Skip to content

Commit

Permalink
Default color types to opaque white
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
waywardmonkeys committed Dec 10, 2024
1 parent 1f8f23f commit 7624122
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions color/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -37,6 +40,9 @@ pub struct OpaqueColor<CS> {
///
/// 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))]
Expand All @@ -60,6 +66,9 @@ pub struct AlphaColor<CS> {
/// 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))]
Expand Down Expand Up @@ -637,6 +646,26 @@ impl<CS: ColorSpace> PremulColor<CS> {
}
}

// Defaults

impl<CS: ColorSpace> Default for AlphaColor<CS> {
fn default() -> Self {
Self::WHITE
}
}

impl<CS: ColorSpace> Default for OpaqueColor<CS> {
fn default() -> Self {
Self::WHITE
}
}

impl<CS: ColorSpace> Default for PremulColor<CS> {
fn default() -> Self {
Self::WHITE
}
}

// Lossless conversion traits.

impl<CS: ColorSpace> From<OpaqueColor<CS>> for AlphaColor<CS> {
Expand Down
14 changes: 14 additions & 0 deletions color/src/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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))]
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7624122

Please sign in to comment.