Skip to content

Commit

Permalink
Update to new peniko with color
Browse files Browse the repository at this point in the history
  • Loading branch information
waywardmonkeys committed Nov 28, 2024
1 parent 7592355 commit cd7363b
Show file tree
Hide file tree
Showing 29 changed files with 327 additions and 243 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ You can find its changes [documented below](#030---2024-10-04).

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

### Highlights

As part of an initiative to improve color handling across the ecosystem (and especially within Linebender crates), Vello is now using the new [`color`] crate.
This is the first step towards providing richer color functionality, better handling of color interpolation, and more.

### Changed

- Breaking: Updated `wgpu` to 23.0.1 ([#735][], [#743][] by [@waywardmonkeys])
- Breaking: Updated to new `peniko` and `color` is now used for all colors ([#742][] by [@waywardmonkeys])

### Fixed

Expand Down Expand Up @@ -202,6 +208,7 @@ This release has an [MSRV][] of 1.75.
[#733]: https://github.com/linebender/vello/pull/733
[#735]: https://github.com/linebender/vello/pull/735
[#740]: https://github.com/linebender/vello/pull/740
[#742]: https://github.com/linebender/vello/pull/742
[#743]: https://github.com/linebender/vello/pull/743

[Unreleased]: https://github.com/linebender/vello/compare/v0.3.0...HEAD
Expand All @@ -214,3 +221,4 @@ This release has an [MSRV][] of 1.75.
[MSRV]: README.md#minimum-supported-rust-version-msrv
[`run_app`]: https://docs.rs/winit/latest/winit/event_loop/struct.EventLoop.html#method.run_app
[stroke-expansion]: https://linebender.org/gpu-stroke-expansion-paper/
[`color`]: https://docs.rs/color/
20 changes: 14 additions & 6 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ bytemuck = { version = "1.18.0", features = ["derive"] }
skrifa = "0.22.3"
# The version of kurbo used below should be kept in sync
# with the version of kurbo used by peniko.
peniko = "0.2.0"
# peniko = "0.2.0"
peniko = { version = "0.2.0", git = "https://github.com/linebender/peniko.git", rev = "cb75a00" }
# FIXME: This can be removed once peniko supports the schemars feature.
kurbo = "0.11.1"
futures-intrusive = "0.5.0"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ let mut scene = Scene::new();
scene.fill(
Fill::NonZero,
Affine::IDENTITY,
Color::rgb8(242, 140, 168),
Color::from_rgba8(242, 140, 168, 255),
None,
&Circle::new((420.0, 200.0), 120.0),
);
Expand All @@ -114,7 +114,7 @@ renderer
&scene,
&surface_texture,
&RenderParams {
base_color: Color::BLACK, // Background color
base_color: palette::css::BLACK, // Background color
width,
height,
antialiasing_method: AaConfig::Msaa16,
Expand Down
3 changes: 2 additions & 1 deletion examples/headless/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use anyhow::{anyhow, bail, Context, Result};
use clap::Parser;
use scenes::{ImageCache, SceneParams, SceneSet, SimpleText};
use vello::kurbo::{Affine, Vec2};
use vello::peniko::color::palette;
use vello::util::RenderContext;
use vello::wgpu::{
self, BufferDescriptor, BufferUsages, CommandEncoderDescriptor, Extent3d, ImageCopyBuffer,
Expand Down Expand Up @@ -145,7 +146,7 @@ async fn render(mut scenes: SceneSet, index: usize, args: &Args) -> Result<()> {
.args
.base_color
.or(scene_params.base_color)
.unwrap_or(vello::peniko::Color::BLACK),
.unwrap_or(palette::css::BLACK),
width,
height,
antialiasing_method: vello::AaConfig::Area,
Expand Down
11 changes: 5 additions & 6 deletions examples/scenes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ mod simple_text;
mod svg;
pub mod test_scenes;

use anyhow::{anyhow, Result};
use clap::Args;
pub use images::ImageCache;
pub use simple_text::SimpleText;
Expand All @@ -41,7 +40,7 @@ pub use svg::{default_scene, scene_from_files};
use test_scenes::test_scenes;

use vello::kurbo::Vec2;
use vello::peniko::Color;
use vello::peniko::{color, Color};
use vello::Scene;

pub struct SceneParams<'a> {
Expand Down Expand Up @@ -93,15 +92,15 @@ pub struct Arguments {
/// The svg files paths to render
svgs: Option<Vec<PathBuf>>,
#[arg(help_heading = "Render Parameters")]
#[arg(long, global(false), value_parser = parse_color)]
#[arg(long, global(false), value_parser = parse_color_arg)]
/// The base color applied as the blend background to the rasterizer.
/// Format is CSS style hexadecimal (#RGB, #RGBA, #RRGGBB, #RRGGBBAA) or
/// an SVG color name such as "aliceblue"
pub base_color: Option<Color>,
}

impl Arguments {
pub fn select_scene_set(&self) -> Result<Option<SceneSet>> {
pub fn select_scene_set(&self) -> anyhow::Result<Option<SceneSet>> {
// There is no file access on WASM, and on Android we haven't set up the assets
// directory.
// TODO: Upload the assets directory on Android
Expand All @@ -120,6 +119,6 @@ impl Arguments {
}
}

fn parse_color(s: &str) -> Result<Color> {
Color::parse(s).ok_or(anyhow!("'{s}' is not a valid color"))
fn parse_color_arg(s: &str) -> Result<Color, color::ParseError> {
color::parse_color(s).map(|c| c.to_alpha_color())
}
14 changes: 7 additions & 7 deletions examples/scenes/src/mmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ impl TestScene for MMark {
}

const COLORS: &[Color] = &[
Color::rgb8(0x10, 0x10, 0x10),
Color::rgb8(0x80, 0x80, 0x80),
Color::rgb8(0xc0, 0xc0, 0xc0),
Color::rgb8(0x10, 0x10, 0x10),
Color::rgb8(0x80, 0x80, 0x80),
Color::rgb8(0xc0, 0xc0, 0xc0),
Color::rgb8(0xe0, 0x10, 0x40),
Color::from_rgba8(0x10, 0x10, 0x10, 0xff),
Color::from_rgba8(0x80, 0x80, 0x80, 0xff),
Color::from_rgba8(0xc0, 0xc0, 0xc0, 0xff),
Color::from_rgba8(0x10, 0x10, 0x10, 0xff),
Color::from_rgba8(0x80, 0x80, 0x80, 0xff),
Color::from_rgba8(0xc0, 0xc0, 0xc0, 0xff),
Color::from_rgba8(0xe0, 0x10, 0x40, 0xff),
];

impl Element {
Expand Down
18 changes: 9 additions & 9 deletions examples/scenes/src/pico_svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::str::FromStr;
use roxmltree::{Document, Node};
use vello::{
kurbo::{Affine, BezPath, Point, Size, Vec2},
peniko::color::{self, palette},
peniko::Color,
};

Expand Down Expand Up @@ -103,7 +104,7 @@ impl PicoSvg {
Affine::new([-scale, 0.0, 0.0, scale, 0.0, 0.0])
};
let props = RecursiveProperties {
fill: Some(Color::BLACK),
fill: Some(palette::css::BLACK),
};
// The root element is the svg document element, which we don't care about
let mut items = Vec::new();
Expand Down Expand Up @@ -260,30 +261,29 @@ fn parse_transform(transform: &str) -> Affine {

fn parse_color(color: &str) -> Color {
let color = color.trim();
if let Some(c) = Color::parse(color) {
c
if let Ok(c) = color::parse_color(color) {
c.to_alpha_color()
} else if let Some(s) = color.strip_prefix("rgb(").and_then(|s| s.strip_suffix(')')) {
let mut iter = s.split([',', ' ']).map(str::trim).map(u8::from_str);

let r = iter.next().unwrap().unwrap();
let g = iter.next().unwrap().unwrap();
let b = iter.next().unwrap().unwrap();
Color::rgb8(r, g, b)
Color::from_rgba8(r, g, b, 255)
} else {
Color::rgba8(255, 0, 255, 0x80)
Color::from_rgba8(255, 0, 255, 0x80)
}
}

fn modify_opacity(mut color: Color, attr_name: &str, node: Node<'_, '_>) -> Color {
fn modify_opacity(color: Color, attr_name: &str, node: Node<'_, '_>) -> Color {
if let Some(opacity) = node.attribute(attr_name) {
let alpha: f64 = if let Some(o) = opacity.strip_suffix('%') {
let alpha: f32 = if let Some(o) = opacity.strip_suffix('%') {
let pctg = o.parse().unwrap_or(100.0);
pctg * 0.01
} else {
opacity.parse().unwrap_or(1.0)
};
color.a = (alpha.clamp(0.0, 1.0) * 255.0).round() as u8;
color
color.with_alpha(alpha.clamp(0., 1.))
} else {
color
}
Expand Down
8 changes: 4 additions & 4 deletions examples/scenes/src/simple_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use std::sync::Arc;

use vello::kurbo::Affine;
use vello::peniko::{Blob, Brush, BrushRef, Color, Font, StyleRef};
use vello::peniko::{color::palette, Blob, Brush, BrushRef, Font, StyleRef};
use vello::skrifa::{raw::FontRef, MetadataProvider};
use vello::{Glyph, Scene};

Expand Down Expand Up @@ -59,7 +59,7 @@ impl SimpleText {
size,
&[],
// This should be unused
&Brush::Solid(Color::WHITE),
&Brush::Solid(palette::css::WHITE),
transform,
glyph_transform,
style,
Expand Down Expand Up @@ -90,7 +90,7 @@ impl SimpleText {
size,
&[],
// This should be unused
&Brush::Solid(Color::WHITE),
&Brush::Solid(palette::css::WHITE),
transform,
glyph_transform,
style,
Expand Down Expand Up @@ -191,7 +191,7 @@ impl SimpleText {
text: &str,
) {
use vello::peniko::Fill;
let brush = brush.unwrap_or(&Brush::Solid(Color::WHITE));
let brush = brush.unwrap_or(&Brush::Solid(palette::css::WHITE));
self.add_run(
scene,
font,
Expand Down
4 changes: 2 additions & 2 deletions examples/scenes/src/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use web_time::Instant;
use anyhow::Result;
use vello::{
kurbo::{Affine, Rect, Stroke, Vec2},
peniko::{Color, Fill},
peniko::{color::palette, Fill},
Scene,
};

Expand Down Expand Up @@ -124,7 +124,7 @@ pub fn svg_function_of<R: AsRef<str>>(
error_scene.fill(
Fill::NonZero,
Affine::IDENTITY,
Color::FUCHSIA,
palette::css::FUCHSIA,
None,
&Rect::new(0.0, 0.0, 1.0, 1.0),
);
Expand Down
Loading

0 comments on commit cd7363b

Please sign in to comment.