Replace the Color type with Table<Color> everywhere (#3048)

This commit is contained in:
Keavon Chambers
2025-08-12 00:38:23 -07:00
committed by GitHub
parent 437fc70500
commit 1b351aca76
46 changed files with 306 additions and 386 deletions

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();