Skip to content

Commit

Permalink
WIP: Converting to new peniko with color.
Browse files Browse the repository at this point in the history
  • Loading branch information
waywardmonkeys committed Nov 24, 2024
1 parent 2f2eaa5 commit c04738a
Show file tree
Hide file tree
Showing 19 changed files with 233 additions and 193 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.76"
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 vello -p vello_encoding -p vello_shaders"
Expand Down
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 @@ -46,7 +46,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 = "c65c325" }
# 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 @@ -11,7 +11,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 @@ -20,7 +19,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 @@ -72,15 +71,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 @@ -99,6 +98,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.map(|r, g, b, _a| [r, g, b, 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 c04738a

Please sign in to comment.