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 27, 2024
1 parent e6ead09 commit 701c5f4
Show file tree
Hide file tree
Showing 20 changed files with 248 additions and 207 deletions.
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
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
17 changes: 8 additions & 9 deletions examples/scenes/src/pico_svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::str::FromStr;
use roxmltree::{Document, Node};
use vello::{
kurbo::{Affine, BezPath, Point, Size, Vec2},
peniko::Color,
peniko::{color, Color},
};

pub struct PicoSvg {
Expand Down Expand Up @@ -260,30 +260,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
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 701c5f4

Please sign in to comment.