Replace the Color type with Table<Color> everywhere (#3048)
Some checks are pending
Editor: Dev & CI / build (push) Waiting to run
Editor: Dev & CI / cargo-deny (push) Waiting to run

This commit is contained in:
Keavon Chambers 2025-08-12 00:38:23 -07:00 committed by GitHub
parent 437fc70500
commit 1b351aca76
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
46 changed files with 306 additions and 386 deletions

View file

@ -130,7 +130,7 @@ where
pub async fn create_brush_texture(brush_style: &BrushStyle) -> Raster<CPU> {
let stamp = brush_stamp_generator(brush_style.diameter, brush_style.color, brush_style.hardness, brush_style.flow);
let transform = DAffine2::from_scale_angle_translation(DVec2::splat(brush_style.diameter), 0., -DVec2::splat(brush_style.diameter / 2.));
let blank_texture = empty_image((), transform, Color::TRANSPARENT).into_iter().next().unwrap_or_default();
let blank_texture = empty_image((), transform, Table::new_from_element(Color::TRANSPARENT)).into_iter().next().unwrap_or_default();
let image = blend_stamp_closure(stamp, blank_texture, |a, b| blend_colors(a, b, BlendMode::Normal, 1.));
image.element
@ -230,7 +230,6 @@ async fn brush(_: impl Ctx, mut image_frame_table: Table<Raster<CPU>>, strokes:
let stroke_origin_in_layer = bbox.start - snap_offset - DVec2::splat(stroke.style.diameter / 2.);
let stroke_to_layer = DAffine2::from_translation(stroke_origin_in_layer) * DAffine2::from_scale(stroke_size);
// let normal_blend = BlendColorPairNode::new(ValueNode::new(CopiedNode::new(BlendMode::Normal)), ValueNode::new(CopiedNode::new(100.)));
let normal_blend = FnNode::new(|(a, b)| blend_colors(a, b, BlendMode::Normal, 1.));
let blit_node = BlitNode::new(
FutureWrapperNode::new(ClonedNode::new(brush_texture)),
@ -241,7 +240,7 @@ async fn brush(_: impl Ctx, mut image_frame_table: Table<Raster<CPU>>, strokes:
let target = core::mem::take(&mut brush_plan.first_stroke_texture);
extend_image_to_bounds((), Table::new_from_row(target), stroke_to_layer)
} else {
empty_image((), stroke_to_layer, Color::TRANSPARENT)
empty_image((), stroke_to_layer, Table::new_from_element(Color::TRANSPARENT))
// EmptyImageNode::new(CopiedNode::new(stroke_to_layer), CopiedNode::new(Color::TRANSPARENT)).eval(())
};

View file

@ -104,7 +104,7 @@ async fn create_artboard<T: Into<Table<Graphic>> + 'n>(
label: String,
location: DVec2,
dimensions: DVec2,
background: Color,
background: Table<Color>,
clip: bool,
) -> Table<Artboard> {
let location = location.as_ivec2();
@ -123,6 +123,9 @@ async fn create_artboard<T: Into<Table<Graphic>> + 'n>(
let dimensions = dimensions.abs();
let background: Option<Color> = background.into();
let background = background.unwrap_or(Color::WHITE);
Table::new_from_element(Artboard {
content,
label,

View file

@ -22,14 +22,11 @@ macro_rules! none_impl {
}
};
}
none_impl!(String);
none_impl!(bool);
none_impl!(f32);
none_impl!(f64);
none_impl!(DVec2);
none_impl!(Option<Color>); // TODO: Remove this?
none_impl!(Vec<Color>); // TODO: Remove this?
none_impl!(String);
impl BoundingBox for Color {
fn bounding_box(&self, _transform: DAffine2, _include_stroke: bool) -> RenderBoundingBox {

View file

@ -1,11 +1,10 @@
use crate::Ctx;
use crate::raster_types::{CPU, Raster};
use crate::table::Table;
use crate::vector::Vector;
use crate::{Color, Ctx};
use glam::{DAffine2, DVec2};
#[node_macro::node(category("Debug"), name("Log to Console"))]
fn log_to_console<T: std::fmt::Debug>(_: impl Ctx, #[implementations(String, bool, f64, u32, u64, DVec2, Table<Vector>, DAffine2, Color, Option<Color>)] value: T) -> T {
fn log_to_console<T: std::fmt::Debug>(_: impl Ctx, #[implementations(bool, f64, u32, u64, DVec2, DAffine2, String)] value: T) -> T {
// KEEP THIS `debug!()` - It acts as the output for the debug node itself
log::debug!("{value:#?}");
value
@ -19,13 +18,13 @@ fn size_of(_: impl Ctx, ty: crate::Type) -> Option<usize> {
/// Meant for debugging purposes, not general use. Wraps the input value in the Some variant of an Option.
#[node_macro::node(category("Debug"))]
fn some<T>(_: impl Ctx, #[implementations(f64, f32, u32, u64, String, Color)] input: T) -> Option<T> {
fn some<T>(_: impl Ctx, #[implementations(f64, f32, u32, u64, String)] input: T) -> Option<T> {
Some(input)
}
/// Meant for debugging purposes, not general use. Unwraps the input value from an Option, returning the default value if the input is None.
#[node_macro::node(category("Debug"))]
fn unwrap_option<T: Default>(_: impl Ctx, #[implementations(Option<f64>, Option<u32>, Option<u64>, Option<String>, Option<Color>)] input: Option<T>) -> T {
fn unwrap_option<T: Default>(_: impl Ctx, #[implementations(Option<f64>, Option<u32>, Option<u64>, Option<String>)] input: Option<T>) -> T {
input.unwrap_or_default()
}

View file

@ -139,6 +139,11 @@ impl From<Option<Color>> for Table<Graphic> {
}
}
}
impl From<Table<Color>> for Option<Color> {
fn from(color: Table<Color>) -> Self {
color.into_iter().next().map(|row| row.element)
}
}
// DAffine2
impl From<DAffine2> for Graphic {
@ -297,8 +302,6 @@ async fn wrap_graphic<T: Into<Graphic> + 'n>(
Table<Raster<CPU>>,
Table<Raster<GPU>>,
Table<Color>,
Color,
Option<Color>,
DAffine2,
)]
content: T,
@ -317,8 +320,6 @@ async fn to_graphic<T: Into<Table<Graphic>> + 'n>(
Table<Raster<CPU>>,
Table<Raster<GPU>>,
Table<Color>,
Color,
Option<Color>,
)]
content: T,
) -> Table<Graphic> {
@ -416,13 +417,16 @@ fn index<T: AtIndex + Clone + Default>(
_: impl Ctx,
/// The collection of data, such as a list or table.
#[implementations(
Vec<Color>,
Vec<Option<Color>>,
Vec<f64>, Vec<u64>,
Vec<f64>,
Vec<u32>,
Vec<u64>,
Vec<DVec2>,
Table<Artboard>,
Table<Graphic>,
Table<Vector>,
Table<Raster<CPU>>,
Table<Graphic>,
Table<Raster<GPU>>,
Table<Color>,
)]
collection: T,
/// The index of the item to retrieve, starting from 0 for the first item.

View file

@ -10,14 +10,14 @@ use crate::{Context, Ctx};
use glam::{DAffine2, DVec2};
#[node_macro::node(category("Type Conversion"))]
fn to_string<T: std::fmt::Debug>(_: impl Ctx, #[implementations(String, bool, f64, u32, u64, DVec2, DAffine2, Table<Vector>)] value: T) -> String {
format!("{:?}", value)
fn to_string<T: std::fmt::Debug>(_: impl Ctx, #[implementations(bool, f64, u32, u64, DVec2, DAffine2, String)] value: T) -> String {
format!("{value:?}")
}
#[node_macro::node(category("Text"))]
fn serialize<T: serde::Serialize>(
_: impl Ctx,
#[implementations(String, bool, f64, u32, u64, DVec2, DAffine2, Color, Option<Color>, Table<Graphic>, Table<Vector>, Table<Raster<CPU>>)] value: T,
#[implementations(String, bool, f64, u32, u64, DVec2, DAffine2, Table<Artboard>, Table<Graphic>, Table<Vector>, Table<Raster<CPU>>, Table<Color>)] value: T,
) -> String {
serde_json::to_string(&value).unwrap_or_else(|_| "Serialization Error".to_string())
}
@ -64,8 +64,7 @@ async fn switch<T, C: Send + 'n + Clone>(
Context -> Table<Vector>,
Context -> Table<Raster<CPU>>,
Context -> Table<Raster<GPU>>,
Context -> Color,
Context -> Option<Color>,
Context -> Table<Color>,
Context -> GradientStops,
)]
if_true: impl Node<C, Output = T>,
@ -84,8 +83,7 @@ async fn switch<T, C: Send + 'n + Clone>(
Context -> Table<Vector>,
Context -> Table<Raster<CPU>>,
Context -> Table<Raster<GPU>>,
Context -> Color,
Context -> Option<Color>,
Context -> Table<Color>,
Context -> GradientStops,
)]
if_false: impl Node<C, Output = T>,

View file

@ -60,3 +60,30 @@ impl Clampable for DVec2 {
self.min(DVec2::splat(max))
}
}
// TODO: Eventually remove this migration document upgrade code
pub fn migrate_color<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result<crate::table::Table<graphene_core_shaders::color::Color>, D::Error> {
use crate::table::Table;
use graphene_core_shaders::color::Color;
use serde::Deserialize;
#[derive(serde::Serialize, serde::Deserialize)]
#[serde(untagged)]
enum EitherFormat {
Color(Color),
OptionalColor(Option<Color>),
ColorTable(Table<Color>),
}
Ok(match EitherFormat::deserialize(deserializer)? {
EitherFormat::Color(color) => Table::new_from_element(color),
EitherFormat::OptionalColor(color) => {
if let Some(color) = color {
Table::new_from_element(color)
} else {
Table::new()
}
}
EitherFormat::ColorTable(color_table) => color_table,
})
}

View file

@ -64,5 +64,3 @@ impl RenderComplexity for bool {}
impl RenderComplexity for f32 {}
impl RenderComplexity for f64 {}
impl RenderComplexity for DVec2 {}
impl RenderComplexity for Option<Color> {}
impl RenderComplexity for Vec<Color> {}

View file

@ -252,6 +252,15 @@ pub struct TableRow<T> {
}
impl<T> TableRow<T> {
pub fn new_from_element(element: T) -> Self {
Self {
element,
transform: DAffine2::IDENTITY,
alpha_blending: AlphaBlending::default(),
source_node_id: None,
}
}
pub fn as_ref(&self) -> TableRowRef<'_, T> {
TableRowRef {
element: &self.element,

View file

@ -2,6 +2,7 @@
use crate::Color;
pub use crate::gradient::*;
use crate::table::Table;
use dyn_any::DynAny;
use glam::DAffine2;
@ -120,6 +121,12 @@ impl From<Option<Color>> for Fill {
}
}
impl From<Table<Color>> for Fill {
fn from(color: Table<Color>) -> Fill {
Fill::solid_or_none(color.into())
}
}
impl From<Gradient> for Fill {
fn from(gradient: Gradient) -> Fill {
Fill::Gradient(gradient)
@ -309,17 +316,6 @@ impl std::hash::Hash for Stroke {
}
}
impl From<Color> for Stroke {
fn from(color: Color) -> Self {
Self::new(Some(color), 1.)
}
}
impl From<Option<Color>> for Stroke {
fn from(color: Option<Color>) -> Self {
Self::new(color, 1.)
}
}
impl Stroke {
pub const fn new(color: Option<Color>, weight: f64) -> Self {
Self {

View file

@ -48,28 +48,28 @@ impl VectorTableIterMut for Table<Vector> {
#[node_macro::node(category("Vector: Style"), path(graphene_core::vector))]
async fn assign_colors<T>(
_: impl Ctx,
/// The content with vector paths to apply the fill and/or stroke style to.
#[implementations(Table<Graphic>, Table<Vector>)]
#[widget(ParsedWidgetOverride::Hidden)]
/// The content with vector paths to apply the fill and/or stroke style to.
mut content: T,
#[default(true)]
/// Whether to style the fill.
#[default(true)]
fill: bool,
/// Whether to style the stroke.
stroke: bool,
#[widget(ParsedWidgetOverride::Custom = "assign_colors_gradient")]
/// The range of colors to select from.
#[widget(ParsedWidgetOverride::Custom = "assign_colors_gradient")]
gradient: GradientStops,
/// Whether to reverse the gradient.
reverse: bool,
/// Whether to randomize the color selection for each element from throughout the gradient.
randomize: bool,
#[widget(ParsedWidgetOverride::Custom = "assign_colors_seed")]
/// The seed used for randomization.
/// Seed to determine unique variations on the randomized color selection.
#[widget(ParsedWidgetOverride::Custom = "assign_colors_seed")]
seed: SeedValue,
#[widget(ParsedWidgetOverride::Custom = "assign_colors_repeat_every")]
/// The number of elements to span across the gradient before repeating. A 0 value will span the entire gradient once.
#[widget(ParsedWidgetOverride::Custom = "assign_colors_repeat_every")]
repeat_every: u32,
) -> T
where
@ -108,32 +108,28 @@ where
#[node_macro::node(category("Vector: Style"), path(graphene_core::vector), properties("fill_properties"))]
async fn fill<F: Into<Fill> + 'n + Send, V: VectorTableIterMut + 'n + Send>(
_: impl Ctx,
/// The content with vector paths to apply the fill style to.
#[implementations(
Table<Vector>,
Table<Vector>,
Table<Vector>,
Table<Vector>,
Table<Graphic>,
Table<Graphic>,
Table<Graphic>,
Table<Graphic>,
)]
/// The content with vector paths to apply the fill style to.
mut content: V,
/// The fill to paint the path with.
#[implementations(
Fill,
Option<Color>,
Color,
Table<Color>,
Gradient,
Fill,
Option<Color>,
Color,
Table<Color>,
Gradient,
)]
#[default(Color::BLACK)]
/// The fill to paint the path with.
fill: F,
_backup_color: Option<Color>,
_backup_color: Table<Color>,
_backup_gradient: Gradient,
) -> V {
let fill: Fill = fill.into();
@ -150,23 +146,17 @@ async fn fill<F: Into<Fill> + 'n + Send, V: VectorTableIterMut + 'n + Send>(
/// Applies a stroke style to the vector contained in the input.
#[node_macro::node(category("Vector: Style"), path(graphene_core::vector), properties("stroke_properties"))]
async fn stroke<C: Into<Option<Color>> + 'n + Send, V>(
async fn stroke<V>(
_: impl Ctx,
#[implementations(Table<Vector>, Table<Vector>, Table<Graphic>, Table<Graphic>)]
/// The content with vector paths to apply the stroke style to.
#[implementations(Table<Vector>, Table<Graphic>)]
mut content: Table<V>,
#[implementations(
Option<Color>,
Color,
Option<Color>,
Color,
)]
#[default(Color::BLACK)]
/// The stroke color.
color: C,
#[default(Color::BLACK)]
color: Table<Color>,
/// The stroke weight.
#[unit(" px")]
#[default(2.)]
/// The stroke weight.
weight: f64,
/// The alignment of stroke to the path's centerline or (for closed shapes) the inside or outside of the shape.
align: StrokeAlign,
@ -174,8 +164,8 @@ async fn stroke<C: Into<Option<Color>> + 'n + Send, V>(
cap: StrokeCap,
/// The curvature of the bent stroke at sharp corners.
join: StrokeJoin,
#[default(4.)]
/// The threshold for when a miter-joined stroke is converted to a bevel-joined stroke when a sharp angle becomes pointier than this ratio.
#[default(4.)]
miter_limit: f64,
/// The order to paint the stroke on top of the fill, or the fill on top of the stroke.
/// <https://svgwg.org/svg2-draft/painting.html#PaintOrderProperty>
@ -286,8 +276,8 @@ async fn circular_repeat<I: 'n + Send + Clone>(
async fn copy_to_points<I: 'n + Send + Clone>(
_: impl Ctx,
points: Table<Vector>,
#[expose]
/// Artwork to be copied and placed at each point.
#[expose]
#[implementations(Table<Graphic>, Table<Vector>, Table<Raster<CPU>>, Table<Color>)]
instance: Table<I>,
/// Minimum range of randomized sizes given to each instance.

View file

@ -1,6 +1,7 @@
use glam::{DAffine2, DVec2};
use graphene_core::gradient::GradientStops;
use graphene_core::registry::types::{Fraction, Percentage, PixelSize, TextArea};
use graphene_core::table::Table;
use graphene_core::transform::Footprint;
use graphene_core::{Color, Ctx, num_traits};
use log::warn;
@ -659,15 +660,16 @@ fn vec2_value(_: impl Ctx, _primary: (), x: f64, y: f64) -> DVec2 {
/// Constructs a color value which may be set to any color, or no color.
#[node_macro::node(category("Value"))]
fn color_value(_: impl Ctx, _primary: (), #[default(Color::BLACK)] color: Option<Color>) -> Option<Color> {
fn color_value(_: impl Ctx, _primary: (), #[default(Color::RED)] color: Table<Color>) -> Table<Color> {
color
}
/// Gets the color at the specified position along the gradient, given a position from 0 (left) to 1 (right).
#[node_macro::node(category("Color"))]
fn sample_gradient(_: impl Ctx, _primary: (), gradient: GradientStops, position: Fraction) -> Color {
fn sample_gradient(_: impl Ctx, _primary: (), gradient: GradientStops, position: Fraction) -> Table<Color> {
let position = position.clamp(0., 1.);
gradient.evaluate(position)
let color = gradient.evaluate(position);
Table::new_from_element(color)
}
/// Constructs a gradient value which may be set to any sequence of color stops to represent the transition between colors.

View file

@ -193,16 +193,15 @@ tagged_value! {
#[cfg_attr(target_family = "wasm", serde(deserialize_with = "graphene_core::artboard::migrate_artboard"))] // TODO: Eventually remove this migration document upgrade code
#[serde(alias = "ArtboardGroup")]
Artboard(Table<Artboard>),
ColorTable(Table<Color>), // TODO: Rename to Color
#[cfg_attr(target_family = "wasm", serde(deserialize_with = "graphene_core::misc::migrate_color"))] // TODO: Eventually remove this migration document upgrade code
#[serde(alias = "ColorTable", alias = "OptionalColor")]
Color(Table<Color>),
// ============
// STRUCT TYPES
// ============
#[serde(alias = "IVec2", alias = "UVec2")]
DVec2(DVec2),
DAffine2(DAffine2),
Color(Color),
OptionalColor(Option<Color>),
Palette(Vec<Color>),
Stroke(graphene_core::vector::style::Stroke),
Gradient(graphene_core::vector::style::Gradient),
#[serde(alias = "GradientPositions")] // TODO: Eventually remove this alias document upgrade code
@ -259,7 +258,6 @@ impl TaggedValue {
TaggedValue::F64(x) => x.to_string() + "_f64",
TaggedValue::Bool(x) => x.to_string(),
TaggedValue::BlendMode(x) => "BlendMode::".to_string() + &x.to_string(),
TaggedValue::Color(x) => format!("Color {x:?}"),
_ => panic!("Cannot convert to primitive string"),
}
}
@ -280,7 +278,7 @@ impl TaggedValue {
6 => return Color::from_rgb_str(color),
8 => return Color::from_rgba_str(color),
_ => {
log::error!("Invalid default value color string: {}", input);
log::error!("Invalid default value color string: {input}");
return None;
}
}
@ -352,8 +350,7 @@ impl TaggedValue {
x if x == TypeId::of::<u32>() => FromStr::from_str(string).map(TaggedValue::U32).ok()?,
x if x == TypeId::of::<DVec2>() => to_dvec2(string).map(TaggedValue::DVec2)?,
x if x == TypeId::of::<bool>() => FromStr::from_str(string).map(TaggedValue::Bool).ok()?,
x if x == TypeId::of::<Color>() => to_color(string).map(TaggedValue::Color)?,
x if x == TypeId::of::<Option<Color>>() => to_color(string).map(|color| TaggedValue::OptionalColor(Some(color)))?,
x if x == TypeId::of::<Table<Color>>() => to_color(string).map(|color| TaggedValue::Color(Table::new_from_element(color)))?,
x if x == TypeId::of::<Fill>() => to_color(string).map(|color| TaggedValue::Fill(Fill::solid(color)))?,
x if x == TypeId::of::<ReferencePoint>() => to_reference_point(string).map(TaggedValue::ReferencePoint)?,
_ => return None,

View file

@ -119,7 +119,6 @@ impl Hash for ConstructionArgs {
}
impl ConstructionArgs {
// TODO: what? Used in the gpu_compiler crate for something.
pub fn new_function_args(&self) -> Vec<String> {
match self {
ConstructionArgs::Nodes(nodes) => nodes.iter().map(|(n, _)| format!("n{:0x}", n.0)).collect(),

View file

@ -8,13 +8,6 @@ impl Adjust<Color> for Color {
*self = map_fn(self);
}
}
impl Adjust<Color> for Option<Color> {
fn adjust(&mut self, map_fn: impl Fn(&Color) -> Color) {
if let Some(color) = self {
*color = map_fn(color)
}
}
}
#[cfg(feature = "std")]
mod adjust_std {
@ -23,13 +16,6 @@ mod adjust_std {
use graphene_core::raster_types::{CPU, Raster};
use graphene_core::table::Table;
impl Adjust<Color> for GradientStops {
fn adjust(&mut self, map_fn: impl Fn(&Color) -> Color) {
for (_, color) in self.iter_mut() {
*color = map_fn(color);
}
}
}
impl Adjust<Color> for Table<Raster<CPU>> {
fn adjust(&mut self, map_fn: impl Fn(&Color) -> Color) {
for row in self.iter_mut() {
@ -39,4 +25,18 @@ mod adjust_std {
}
}
}
impl Adjust<Color> for Table<Color> {
fn adjust(&mut self, map_fn: impl Fn(&Color) -> Color) {
for color in self.iter_mut() {
*color.element = map_fn(color.element);
}
}
}
impl Adjust<Color> for GradientStops {
fn adjust(&mut self, map_fn: impl Fn(&Color) -> Color) {
for (_, color) in self.iter_mut() {
*color = map_fn(color);
}
}
}
}

View file

@ -44,8 +44,8 @@ pub enum LuminanceCalculation {
fn luminance<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
GradientStops,
)]
mut input: T,
@ -68,8 +68,8 @@ fn luminance<T: Adjust<Color>>(
fn gamma_correction<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
GradientStops,
)]
mut input: T,
@ -88,8 +88,8 @@ fn gamma_correction<T: Adjust<Color>>(
fn extract_channel<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
GradientStops,
)]
mut input: T,
@ -111,8 +111,8 @@ fn extract_channel<T: Adjust<Color>>(
fn make_opaque<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
GradientStops,
)]
mut input: T,
@ -136,8 +136,8 @@ fn make_opaque<T: Adjust<Color>>(
fn brightness_contrast<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
GradientStops,
)]
mut input: T,
@ -225,8 +225,8 @@ fn brightness_contrast<T: Adjust<Color>>(
fn levels<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
GradientStops,
)]
mut image: T,
@ -292,12 +292,12 @@ fn levels<T: Adjust<Color>>(
async fn black_and_white<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
GradientStops,
)]
mut image: T,
#[default(Color::BLACK)] tint: Color,
#[default(Color::BLACK)] tint: Table<Color>,
#[default(40.)]
#[range((-200., 300.))]
reds: Percentage,
@ -317,6 +317,9 @@ async fn black_and_white<T: Adjust<Color>>(
#[range((-200., 300.))]
magentas: Percentage,
) -> T {
let tint: Option<Color> = tint.into();
let tint = tint.unwrap_or(Color::BLACK);
image.adjust(|color| {
let color = color.to_gamma_srgb();
@ -364,8 +367,8 @@ async fn black_and_white<T: Adjust<Color>>(
async fn hue_saturation<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
GradientStops,
)]
mut input: T,
@ -398,8 +401,8 @@ async fn hue_saturation<T: Adjust<Color>>(
async fn invert<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
GradientStops,
)]
mut input: T,
@ -420,8 +423,8 @@ async fn invert<T: Adjust<Color>>(
async fn threshold<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
GradientStops,
)]
mut image: T,
@ -465,8 +468,8 @@ async fn threshold<T: Adjust<Color>>(
async fn vibrance<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
GradientStops,
)]
mut image: T,
@ -630,8 +633,8 @@ pub enum DomainWarpType {
async fn channel_mixer<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
GradientStops,
)]
mut image: T,
@ -758,8 +761,8 @@ pub enum SelectiveColorChoice {
async fn selective_color<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
GradientStops,
)]
mut image: T,
@ -900,8 +903,8 @@ async fn selective_color<T: Adjust<Color>>(
async fn posterize<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
GradientStops,
)]
mut input: T,
@ -933,8 +936,8 @@ async fn posterize<T: Adjust<Color>>(
async fn exposure<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
GradientStops,
)]
mut input: T,

View file

@ -17,15 +17,6 @@ impl Blend<Color> for Color {
blend_fn(*self, *under)
}
}
impl Blend<Color> for Option<Color> {
fn blend(&self, under: &Self, blend_fn: impl Fn(Color, Color) -> Color) -> Self {
match (self, under) {
(Some(a), Some(b)) => Some(blend_fn(*a, *b)),
(a, None) => *a,
(None, b) => *b,
}
}
}
#[cfg(feature = "std")]
mod blend_std {
@ -51,6 +42,15 @@ mod blend_std {
result_table
}
}
impl Blend<Color> for Table<Color> {
fn blend(&self, under: &Self, blend_fn: impl Fn(Color, Color) -> Color) -> Self {
let mut result_table = self.clone();
for (over, under) in result_table.iter_mut().zip(under.iter()) {
*over.element = blend_fn(*over.element, *under.element);
}
result_table
}
}
impl Blend<Color> for GradientStops {
fn blend(&self, under: &Self, blend_fn: impl Fn(Color, Color) -> Color) -> Self {
let mut combined_stops = self.iter().map(|(position, _)| position).chain(under.iter().map(|(position, _)| position)).collect::<Vec<_>>();
@ -126,15 +126,15 @@ pub fn apply_blend_mode(foreground: Color, background: Color, blend_mode: BlendM
async fn blend<T: Blend<Color> + Send>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
GradientStops,
)]
over: T,
#[expose]
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
GradientStops,
)]
under: T,
@ -148,17 +148,20 @@ async fn blend<T: Blend<Color> + Send>(
fn color_overlay<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
GradientStops,
)]
mut image: T,
#[default(Color::BLACK)] color: Color,
#[default(Color::BLACK)] color: Table<Color>,
blend_mode: BlendMode,
#[default(100.)] opacity: Percentage,
) -> T {
let opacity = (opacity as f32 / 100.).clamp(0., 1.);
let color: Option<Color> = color.into();
let color = color.unwrap_or(Color::BLACK);
image.adjust(|pixel| {
let image = pixel.map_rgb(|channel| channel * (1. - opacity));
@ -171,18 +174,6 @@ fn color_overlay<T: Adjust<Color>>(
image
}
#[cfg(feature = "std")]
#[node_macro::node(category(""), skip_impl)]
fn blend_color_pair<BlendModeNode, OpacityNode>(input: (Color, Color), blend_mode: &'n BlendModeNode, opacity: &'n OpacityNode) -> Color
where
BlendModeNode: graphene_core::Node<'n, (), Output = BlendMode> + 'n,
OpacityNode: graphene_core::Node<'n, (), Output = Percentage> + 'n,
{
let blend_mode = blend_mode.eval(());
let opacity = opacity.eval(());
blend_colors(input.0, input.1, blend_mode, opacity / 100.)
}
#[cfg(all(feature = "std", test))]
mod test {
use graphene_core::blending::BlendMode;
@ -202,7 +193,13 @@ mod test {
// 100% of the output should come from the multiplied value
let opacity = 100_f64;
let result = super::color_overlay((), Table::new_from_element(Raster::new_cpu(image.clone())), overlay_color, BlendMode::Multiply, opacity);
let result = super::color_overlay(
(),
Table::new_from_element(Raster::new_cpu(image.clone())),
Table::new_from_element(overlay_color),
BlendMode::Multiply,
opacity,
);
let result = result.iter().next().unwrap().element;
// The output should just be the original green and alpha channels (as we multiply them by 1 and other channels by 0)

View file

@ -13,8 +13,8 @@ use graphene_core::{Color, Ctx};
async fn gradient_map<T: Adjust<Color>>(
_: impl Ctx,
#[implementations(
Color,
Table<Raster<CPU>>,
Table<Color>,
GradientStops,
)]
mut image: T,

View file

@ -1,7 +1,7 @@
use graphene_core::color::Color;
use graphene_core::context::Ctx;
use graphene_core::raster_types::{CPU, Raster};
use graphene_core::table::Table;
use graphene_core::table::{Table, TableRow};
#[node_macro::node(category("Color"))]
async fn image_color_palette(
@ -10,13 +10,13 @@ async fn image_color_palette(
#[hard_min(1.)]
#[soft_max(28.)]
max_size: u32,
) -> Vec<Color> {
) -> Table<Color> {
const GRID: f32 = 3.;
let bins = GRID * GRID * GRID;
let mut histogram: Vec<usize> = vec![0; (bins + 1.) as usize];
let mut colors: Vec<Vec<Color>> = vec![vec![]; (bins + 1.) as usize];
let mut histogram = vec![0; (bins + 1.) as usize];
let mut color_bins = vec![Vec::new(); (bins + 1.) as usize];
for row in image.iter() {
for pixel in row.element.data.iter() {
@ -27,40 +27,38 @@ async fn image_color_palette(
let bin = (r * GRID + g * GRID + b * GRID) as usize;
histogram[bin] += 1;
colors[bin].push(pixel.to_gamma_srgb());
color_bins[bin].push(pixel.to_gamma_srgb());
}
}
let shorted = histogram.iter().enumerate().filter(|&(_, &count)| count > 0).map(|(i, _)| i).collect::<Vec<usize>>();
let mut palette = vec![];
shorted
.iter()
.take(max_size as usize)
.flat_map(|&i| {
let list = &color_bins[i];
for i in shorted.iter().take(max_size as usize) {
let list = colors[*i].clone();
let mut r = 0.;
let mut g = 0.;
let mut b = 0.;
let mut a = 0.;
let mut r = 0.;
let mut g = 0.;
let mut b = 0.;
let mut a = 0.;
for color in list.iter() {
r += color.r();
g += color.g();
b += color.b();
a += color.a();
}
for color in list.iter() {
r += color.r();
g += color.g();
b += color.b();
a += color.a();
}
r /= list.len() as f32;
g /= list.len() as f32;
b /= list.len() as f32;
a /= list.len() as f32;
r /= list.len() as f32;
g /= list.len() as f32;
b /= list.len() as f32;
a /= list.len() as f32;
let color = Color::from_rgbaf32(r, g, b, a).unwrap();
palette.push(color);
}
palette
Color::from_rgbaf32(r, g, b, a).map(TableRow::new_from_element).into_iter()
})
.collect()
}
#[cfg(test)]
@ -81,6 +79,6 @@ mod test {
})),
1,
);
assert_eq!(futures::executor::block_on(result), [Color::from_rgbaf32(0., 0., 0., 1.).unwrap()]);
assert_eq!(futures::executor::block_on(result), Table::new_from_element(Color::from_rgbaf32(0., 0., 0., 1.).unwrap()));
}
}

View file

@ -246,7 +246,7 @@ pub fn extend_image_to_bounds(_: impl Ctx, image: Table<Raster<CPU>>, bounds: DA
let image_data = &row.element.data;
let (image_width, image_height) = (row.element.width, row.element.height);
if image_width == 0 || image_height == 0 {
return empty_image((), bounds, Color::TRANSPARENT).into_iter().next().unwrap();
return empty_image((), bounds, Table::new_from_element(Color::TRANSPARENT)).into_iter().next().unwrap();
}
let orig_image_scale = DVec2::new(image_width as f64, image_height as f64);
@ -280,11 +280,12 @@ pub fn extend_image_to_bounds(_: impl Ctx, image: Table<Raster<CPU>>, bounds: DA
}
#[node_macro::node(category("Debug: Raster"))]
pub fn empty_image(_: impl Ctx, transform: DAffine2, color: Color) -> Table<Raster<CPU>> {
pub fn empty_image(_: impl Ctx, transform: DAffine2, color: Table<Color>) -> Table<Raster<CPU>> {
let width = transform.transform_vector2(DVec2::new(1., 0.)).length() as u32;
let height = transform.transform_vector2(DVec2::new(0., 1.)).length() as u32;
let image = Image::new(width, height, color);
let color: Option<Color> = color.into();
let image = Image::new(width, height, color.unwrap_or(Color::WHITE));
let mut result_table = Table::new_from_element(Raster::new_cpu(image));
let row = result_table.get_mut(0).unwrap();

View file

@ -291,8 +291,6 @@ async fn render<'a: 'n, T: 'n + Render + WasmNotSend>(
Context -> Table<Vector>,
Context -> Table<Raster<CPU>>,
Context -> Table<Color>,
Context -> Option<Color>,
Context -> Vec<Color>,
Context -> bool,
Context -> f32,
Context -> f64,

View file

@ -1309,13 +1309,12 @@ impl Render for Table<Color> {
}
}
/// Used to stop rust complaining about upstream traits adding display implementations to `Option<Color>`. This would not be an issue as we control that crate.
trait Primitive: std::fmt::Display + BoundingBox + RenderComplexity {}
impl Primitive for String {}
impl Primitive for bool {}
impl Primitive for f32 {}
impl Primitive for f64 {}
impl Primitive for DVec2 {}
impl Primitive for String {}
fn text_attributes(attributes: &mut SvgRenderAttrs) {
attributes.push("fill", "white");
@ -1332,73 +1331,6 @@ impl<P: Primitive> Render for P {
fn render_to_vello(&self, _scene: &mut Scene, _transform: DAffine2, _context: &mut RenderContext, _render_params: &RenderParams) {}
}
impl Render for Option<Color> {
fn render_svg(&self, render: &mut SvgRender, render_params: &RenderParams) {
if let Some(color) = self {
color.render_svg(render, render_params);
}
}
#[cfg(feature = "vello")]
fn render_to_vello(&self, scene: &mut Scene, parent_transform: DAffine2, _context: &mut RenderContext, render_params: &RenderParams) {
if let Some(color) = self {
color.render_to_vello(scene, parent_transform, _context, render_params);
}
}
}
impl Render for Color {
fn render_svg(&self, render: &mut SvgRender, render_params: &RenderParams) {
render.leaf_tag("rect", |attributes| {
attributes.push("width", render_params.footprint.resolution.x.to_string());
attributes.push("height", render_params.footprint.resolution.y.to_string());
let matrix = format_transform_matrix(render_params.footprint.transform.inverse());
if !matrix.is_empty() {
attributes.push("transform", matrix);
}
attributes.push("fill", format!("#{}", self.to_rgb_hex_srgb_from_gamma()));
if self.a() < 1. {
attributes.push("fill-opacity", ((self.a() * 1000.).round() / 1000.).to_string());
}
});
}
#[cfg(feature = "vello")]
fn render_to_vello(&self, scene: &mut Scene, parent_transform: DAffine2, _context: &mut RenderContext, render_params: &RenderParams) {
let transform = parent_transform * render_params.footprint.transform.inverse();
let vello_color = peniko::Color::new([self.r(), self.g(), self.b(), self.a()]);
let rect = kurbo::Rect::from_origin_size(
kurbo::Point::ZERO,
kurbo::Size::new(render_params.footprint.resolution.x as f64, render_params.footprint.resolution.y as f64),
);
scene.fill(peniko::Fill::NonZero, kurbo::Affine::new(transform.to_cols_array()), vello_color, None, &rect);
}
}
impl Render for Vec<Color> {
fn render_svg(&self, render: &mut SvgRender, _render_params: &RenderParams) {
for (index, &color) in self.iter().enumerate() {
render.leaf_tag("rect", |attributes| {
attributes.push("width", "100");
attributes.push("height", "100");
attributes.push("x", (index * 120).to_string());
attributes.push("y", "40");
attributes.push("fill", format!("#{}", color.to_rgb_hex_srgb_from_gamma()));
if color.a() < 1. {
attributes.push("fill-opacity", ((color.a() * 1000.).round() / 1000.).to_string());
}
});
}
}
#[cfg(feature = "vello")]
fn render_to_vello(&self, _scene: &mut Scene, _transform: DAffine2, _context: &mut RenderContext, _render_params: &RenderParams) {}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SvgSegment {
Slice(&'static str),

View file

@ -63,8 +63,6 @@ fn node_registry() -> HashMap<ProtoNodeIdentifier, HashMap<NodeIOTypes, NodeCons
#[cfg(feature = "gpu")]
async_node!(graphene_core::memo::MonitorNode<_, _, _>, input: Context, fn_params: [Context => Table<Raster<GPU>>]),
async_node!(graphene_core::memo::MonitorNode<_, _, _>, input: Context, fn_params: [Context => Table<Color>]),
async_node!(graphene_core::memo::MonitorNode<_, _, _>, input: Context, fn_params: [Context => Color]),
async_node!(graphene_core::memo::MonitorNode<_, _, _>, input: Context, fn_params: [Context => Option<Color>]),
async_node!(graphene_core::memo::MonitorNode<_, _, _>, input: Context, fn_params: [Context => String]),
async_node!(graphene_core::memo::MonitorNode<_, _, _>, input: Context, fn_params: [Context => IVec2]),
async_node!(graphene_core::memo::MonitorNode<_, _, _>, input: Context, fn_params: [Context => DVec2]),
@ -95,7 +93,6 @@ fn node_registry() -> HashMap<ProtoNodeIdentifier, HashMap<NodeIOTypes, NodeCons
async_node!(graphene_core::memo::MonitorNode<_, _, _>, input: Context, fn_params: [Context => [f64; 4]]),
async_node!(graphene_core::memo::MonitorNode<_, _, _>, input: Context, fn_params: [Context => Vec<NodeId>]),
async_node!(graphene_core::memo::MonitorNode<_, _, _>, input: Context, fn_params: [Context => Graphic]),
async_node!(graphene_core::memo::MonitorNode<_, _, _>, input: Context, fn_params: [Context => Vec<Color>]),
async_node!(graphene_core::memo::MonitorNode<_, _, _>, input: Context, fn_params: [Context => graphene_core::text::Font]),
async_node!(graphene_core::memo::MonitorNode<_, _, _>, input: Context, fn_params: [Context => Vec<BrushStroke>]),
async_node!(graphene_core::memo::MonitorNode<_, _, _>, input: Context, fn_params: [Context => BrushCache]),

View file

@ -1271,15 +1271,16 @@ mod tests {
let tuples = quote_spanned!(problem_span=> () ());
let input = quote! {
fn test_node(
#[implementations((), #tuples, Footprint)] footprint: F,
#[implementations((), #tuples, Footprint)]
footprint: F,
#[implementations(
() -> Color,
() -> Table<Raster<CPU>>,
() -> GradientStops,
Footprint -> Color,
Footprint -> Table<Raster<CPU>>,
Footprint -> GradientStops,
)]
() -> Table<Raster<CPU>>,
() -> Table<Color>,
() -> GradientStops,
Footprint -> Table<Raster<CPU>>,
Footprint -> Table<Color>,
Footprint -> GradientStops,
)]
image: impl Node<F, Output = T>,
) -> T {
// Implementation details...