Skip to content

Commit

Permalink
Use color for color.
Browse files Browse the repository at this point in the history
Color functionality is now provided by the `color` crate.
  • Loading branch information
waywardmonkeys committed Nov 27, 2024
1 parent e8a7e5d commit ab9f540
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 728 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ env:
# If the compilation fails, then the version specified here needs to be bumped up to reality.
# Be sure to also update the rust-version property in the workspace Cargo.toml file,
# plus all the README.md files of the affected packages.
RUST_MIN_VER: "1.70"
RUST_MIN_VER: "1.82"
# List of packages that will be checked with the minimum supported Rust version.
# This should be limited to packages that are intended for publishing.
RUST_MIN_VER_PKGS: "-p peniko"
Expand Down
23 changes: 22 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,30 @@ You can find its changes [documented below](#020-2024-09-19).

## [Unreleased]

This release has an [MSRV] of 1.70.
This release has an [MSRV] of 1.82.

### Changed

- `Image` now stores the alpha as an `f32` ([#65][] by [@waywardmonkeys][])
- Use `color` crate. See below for details ([#63][] by [@waywardmonkeys][])

### Color Changes

The old code behind `peniko::Color` has been removed and color functionality is now provided by the [`color`] crate.

This leads to a number of breaking changes:

- `peniko::Color` is now a type alias for `AlphaColor<Srgb>` from the `color` crate.
- `AlphaColor` does not, at this time, impl `Default`, `PartialEq`, `PartialOrd`, or `Hash`.
- `Brush` and `BrushRef` no longer impl `PartialEq`.
- `ColorStop` no longer impls `Default` or `PartialOrd`.
- `Brush`, `BrushRef`, and `ColorStop` can be constructed from a variety of color types, although, for now, `Brush` and `BrushRef` convert this internally into an unclipped `AlphaColor<Srgb>`.
- The `color` crate is re-exported as `peniko::color`, so access to functionality from there is easy.
- The various pre-defined color constants like `peniko::Color::YELLOW` are no longer available.
Instead, use the CSS palette provided within `color`: `peniko::color::palette::css::YELLOW`.
- Similarly, parsing a color string is now provided by the `color` crate.

This is the first step towards providing better support for richer color functionality throughout the Linebender stack.

## [0.2.0][] (2024-09-19)

Expand Down Expand Up @@ -48,12 +67,14 @@ This release has an [MSRV] of 1.70.
- Initial release

[MSRV]: README.md#minimum-supported-rust-version-msrv
[`color`]: https://docs.rs/color/

[#26]: https://github.com/linebender/peniko/pull/26
[#40]: https://github.com/linebender/peniko/pull/40
[#46]: https://github.com/linebender/peniko/pull/46
[#47]: https://github.com/linebender/peniko/pull/47
[#52]: https://github.com/linebender/peniko/pull/52
[#63]: https://github.com/linebender/peniko/pull/63
[#65]: https://github.com/linebender/peniko/pull/65

[@DJMcNab]: https://github.com/DJMcNab
Expand Down
30 changes: 20 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 10 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ keywords = ["graphics", "vector", "style"]
categories = ["graphics"]
repository = "https://github.com/linebender/peniko"
readme = "README.md"
# We support from Rust 1.70 so that CI uses the sparse protocol.
# Keep in sync with RUST_MIN_VER in .github/workflows/ci.yml and with the relevant README.md files.
# and with the MSRV in the `Unreleased` section of CHANGELOG.md.
rust-version = "1.70"
rust-version = "1.82"

[package.metadata.docs.rs]
all-features = true
Expand All @@ -21,16 +20,22 @@ targets = []

[features]
default = ["std"]
std = ["kurbo/std"]
libm = ["kurbo/libm"]
std = ["color/std", "kurbo/std"]
libm = ["color/libm", "kurbo/libm"]
mint = ["kurbo/mint"]
serde = ["smallvec/serde", "kurbo/serde", "dep:serde_bytes", "dep:serde"]
serde = ["color/serde", "smallvec/serde", "kurbo/serde", "dep:serde_bytes", "dep:serde"]

[dependencies]
# NOTE: Make sure to keep this in sync with the version badge in README.md
kurbo = { version = "0.11.1", default-features = false }
smallvec = "1.13.2"

[dependencies.color]
git = "https://github.com/linebender/color.git"
rev = "bccd4050607eba830cfba3b2f39616a27500288a"
version = "0.1.0"
default-features = false

[dependencies.serde]
version = "1.0.210"
optional = true
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@

</div>

The Peniko library builds on top of [kurbo] and provides a set of generic types that define
styles for rendering and composition.
The Peniko library builds on top of [kurbo] and [color] and provides a set of generic types that define styles for rendering and composition.

The name "Peniko" is Esperanto for "brush" which is one family of types that the library
contains.

## Minimum supported Rust Version (MSRV)

This version of Peniko has been verified to compile with **Rust 1.70** and later.
This version of Peniko has been verified to compile with **Rust 1.82** and later.

Future versions of Peniko might increase the Rust version requirement.
It will not be treated as a breaking change and as such can even happen with small patch releases.
Expand Down Expand Up @@ -66,6 +65,7 @@ Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
licensed as above, without any additional terms or conditions.

[color]: https://crates.io/crates/color
[kurbo]: https://crates.io/crates/kurbo
[Rust Code of Conduct]: https://www.rust-lang.org/policies/code-of-conduct
[AUTHORS]: ./AUTHORS
66 changes: 54 additions & 12 deletions src/brush.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

use super::{Color, Gradient, Image};

use color::{AlphaColor, ColorSpace, DynamicColor, OpaqueColor, Srgb};

/// Describes the color content of a filled or stroked shape.
///
/// See also [`BrushRef`] which can be used to avoid allocations.
#[derive(Clone, PartialEq, Debug)]
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Brush {
/// Solid color brush.
Expand All @@ -17,9 +19,21 @@ pub enum Brush {
Image(Image),
}

impl From<Color> for Brush {
fn from(c: Color) -> Self {
Self::Solid(c)
impl<CS: ColorSpace> From<AlphaColor<CS>> for Brush {
fn from(c: AlphaColor<CS>) -> Self {
Self::Solid(c.convert())
}
}

impl From<DynamicColor> for Brush {
fn from(c: DynamicColor) -> Self {
Self::Solid(c.to_alpha_color::<Srgb>())
}
}

impl<CS: ColorSpace> From<OpaqueColor<CS>> for Brush {
fn from(c: OpaqueColor<CS>) -> Self {
Self::Solid(c.with_alpha(1.).convert())
}
}

Expand All @@ -37,7 +51,7 @@ impl From<Image> for Brush {

impl Default for Brush {
fn default() -> Self {
Self::Solid(Color::default())
Self::Solid(Color::TRANSPARENT)
}
}

Expand Down Expand Up @@ -77,7 +91,11 @@ impl Brush {
/// This is useful for methods that would like to accept brushes by reference. Defining
/// the type as `impl<Into<BrushRef>>` allows accepting types like `&LinearGradient`
/// directly without cloning or allocating.
#[derive(Copy, Clone, PartialEq, Debug)]
#[cfg_attr(
target_pointer_width = "32",
expect(variant_size_differences, reason = "We're okay with this.")
)]
#[derive(Copy, Clone, Debug)]
pub enum BrushRef<'a> {
/// Solid color brush.
Solid(Color),
Expand All @@ -99,15 +117,39 @@ impl BrushRef<'_> {
}
}

impl From<Color> for BrushRef<'_> {
fn from(color: Color) -> Self {
Self::Solid(color)
impl<CS: ColorSpace> From<AlphaColor<CS>> for BrushRef<'_> {
fn from(color: AlphaColor<CS>) -> Self {
Self::Solid(color.convert())
}
}

impl From<DynamicColor> for BrushRef<'_> {
fn from(color: DynamicColor) -> Self {
Self::Solid(color.to_alpha_color::<Srgb>())
}
}

impl<CS: ColorSpace> From<OpaqueColor<CS>> for BrushRef<'_> {
fn from(color: OpaqueColor<CS>) -> Self {
Self::Solid(color.with_alpha(1.).convert())
}
}

impl<'a, CS: ColorSpace> From<&'a AlphaColor<CS>> for BrushRef<'_> {
fn from(color: &'a AlphaColor<CS>) -> Self {
Self::Solid((*color).convert())
}
}

impl<'a> From<&'a DynamicColor> for BrushRef<'_> {
fn from(color: &'a DynamicColor) -> Self {
Self::Solid((*color).to_alpha_color::<Srgb>())
}
}

impl<'a> From<&'a Color> for BrushRef<'_> {
fn from(color: &'a Color) -> Self {
Self::Solid(*color)
impl<'a, CS: ColorSpace> From<&'a OpaqueColor<CS>> for BrushRef<'_> {
fn from(color: &'a OpaqueColor<CS>) -> Self {
Self::Solid((*color).with_alpha(1.).convert())
}
}

Expand Down
Loading

0 comments on commit ab9f540

Please sign in to comment.