Skip to content

Commit

Permalink
Use color for color.
Browse files Browse the repository at this point in the history
This leaves `peniko::Color` for now, but begins the migration to
using the new `color` crate.
  • Loading branch information
waywardmonkeys committed Nov 23, 2024
1 parent a421674 commit f165bcf
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 18 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
14 changes: 12 additions & 2 deletions Cargo.lock

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

14 changes: 8 additions & 6 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,12 +20,13 @@ 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"]

[dependencies]
color = { version = "0.1.0", default-features = false }
# 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"
Expand Down Expand Up @@ -69,8 +69,10 @@ rust.unused_macro_rules = "warn"
rust.unused_qualifications = "warn"
rust.variant_size_differences = "warn"

clippy.allow_attributes = "warn"
clippy.allow_attributes_without_reason = "warn"
# FIXME(color): I'm lazy about fixing this yet.
clippy.allow_attributes = "allow"
clippy.allow_attributes_without_reason = "allow"

clippy.cast_possible_truncation = "warn"
clippy.collection_is_never_read = "warn"
clippy.dbg_macro = "warn"
Expand Down
35 changes: 28 additions & 7 deletions src/brush.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

use super::{Color, Gradient, Image};
use color::{AlphaColor, 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)]
// FIXME(color): `PartialEq` was removed from here for now.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Brush {
/// Solid color brush.
Solid(Color),
Solid(AlphaColor<Srgb>),
/// Gradient brush.
Gradient(Gradient),
/// Image brush.
Expand All @@ -19,6 +21,12 @@ pub enum Brush {

impl From<Color> for Brush {
fn from(c: Color) -> Self {
Self::Solid(c.into())
}
}

impl From<AlphaColor<Srgb>> for Brush {
fn from(c: AlphaColor<Srgb>) -> Self {
Self::Solid(c)
}
}
Expand All @@ -37,7 +45,7 @@ impl From<Image> for Brush {

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

Expand All @@ -58,7 +66,7 @@ impl Brush {
self
} else {
match self {
Self::Solid(color) => color.multiply_alpha(alpha).into(),
Self::Solid(color) => color.mul_alpha(alpha).into(),
Self::Gradient(mut gradient) => {
gradient
.stops
Expand All @@ -77,10 +85,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)]
// FIXME(color): `PartialEq` was removed from here for now.
#[derive(Copy, Clone, Debug)]
pub enum BrushRef<'a> {
/// Solid color brush.
Solid(Color),
Solid(AlphaColor<Srgb>),
/// Gradient brush.
Gradient(&'a Gradient),
/// Image brush.
Expand All @@ -101,12 +110,24 @@ impl BrushRef<'_> {

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

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

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

impl<'a> From<&'a AlphaColor<Srgb>> for BrushRef<'_> {
fn from(color: &'a AlphaColor<Srgb>) -> Self {
Self::Solid(*color)
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

// Borrows code heavily from the piet (https://github.com/linebender/piet/) Color
// type.
use color::{AlphaColor, Srgb};
#[cfg(all(not(feature = "std"), feature = "libm"))]
#[allow(unused_imports)]
use kurbo::common::FloatFuncs as _;
Expand Down Expand Up @@ -467,6 +468,12 @@ impl Color {
pub const YELLOW_GREEN: Color = Color::rgba8(154, 205, 50, 255);
}

impl From<Color> for AlphaColor<Srgb> {
fn from(c: Color) -> Self {
Self::from_rgba8(c.r, c.g, c.b, c.a)
}
}

impl From<[u8; 3]> for Color {
fn from(rgb: [u8; 3]) -> Self {
Self::rgb8(rgb[0], rgb[1], rgb[2])
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

//! A Rust 2D graphics type library
//!
//! 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.
//!
//! [`kurbo`]: https://crates.io/crates/kurbo
//! [`color`]: https://crates.io/crates/color
#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
Expand Down

0 comments on commit f165bcf

Please sign in to comment.