mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-15 22:28:10 +08:00
New nodes: RGBA to Color, HSVA to Color, Hex to Color, and Read Gradient (#3838)
* New nodes: RGBA to Color, HSVA to Color, Hex to Color, and Read Gradient * Simplify
This commit is contained in:
@@ -2,6 +2,7 @@ use core_types::table::Table;
|
||||
use core_types::{Color, ExtractVarArgs};
|
||||
use core_types::{Ctx, ExtractIndex, ExtractPosition};
|
||||
use glam::DVec2;
|
||||
use graphic_types::vector_types::GradientStops;
|
||||
use graphic_types::{Graphic, Vector};
|
||||
use raster_types::{CPU, Raster};
|
||||
|
||||
@@ -37,6 +38,14 @@ fn read_color(ctx: impl Ctx + ExtractVarArgs) -> Table<Color> {
|
||||
var_arg.downcast_ref().cloned().unwrap_or_default()
|
||||
}
|
||||
|
||||
#[node_macro::node(category("Context"), path(graphene_core::vector))]
|
||||
fn read_gradient(ctx: impl Ctx + ExtractVarArgs) -> Table<GradientStops> {
|
||||
let Ok(var_arg) = ctx.vararg(0) else { return Default::default() };
|
||||
let var_arg = var_arg as &dyn std::any::Any;
|
||||
|
||||
var_arg.downcast_ref().cloned().unwrap_or_default()
|
||||
}
|
||||
|
||||
#[node_macro::node(category("Context"), path(core_types::vector))]
|
||||
async fn read_position(
|
||||
ctx: impl Ctx + ExtractPosition,
|
||||
|
||||
@@ -765,6 +765,48 @@ fn color_value(_: impl Ctx, _primary: (), #[default(Color::BLACK)] color: Table<
|
||||
color
|
||||
}
|
||||
|
||||
/// Constructs a color value from red, green, blue, and alpha components given as numbers from 0 to 1.
|
||||
#[node_macro::node(category("Color"), name("RGBA to Color"))]
|
||||
fn rgba_to_color(_: impl Ctx, _primary: (), red: Fraction, green: Fraction, blue: Fraction, #[default(1.)] alpha: Fraction) -> Table<Color> {
|
||||
let red = (red as f32).clamp(0., 1.);
|
||||
let green = (green as f32).clamp(0., 1.);
|
||||
let blue = (blue as f32).clamp(0., 1.);
|
||||
let alpha = (alpha as f32).clamp(0., 1.);
|
||||
|
||||
Table::new_from_element(Color::from_rgbaf32_unchecked(red, green, blue, alpha))
|
||||
}
|
||||
|
||||
/// Constructs a color value from hue, saturation, value, and alpha components given as numbers from 0 to 1.
|
||||
#[node_macro::node(category("Color"), name("HSVA to Color"))]
|
||||
fn hsva_to_color(_: impl Ctx, _primary: (), hue: Fraction, #[default(1.)] saturation: Fraction, #[default(1.)] value: Fraction, #[default(1.)] alpha: Fraction) -> Table<Color> {
|
||||
let hue = (hue as f32) - (hue as f32).floor();
|
||||
let saturation = (saturation as f32).clamp(0., 1.);
|
||||
let value = (value as f32).clamp(0., 1.);
|
||||
let alpha = (alpha as f32).clamp(0., 1.);
|
||||
|
||||
Table::new_from_element(Color::from_hsva(hue, saturation, value, alpha))
|
||||
}
|
||||
|
||||
/// Constructs a color value from hue, saturation, lightness, and alpha components given as numbers from 0 to 1.
|
||||
#[node_macro::node(category("Color"), name("HSLA to Color"))]
|
||||
fn hsla_to_color(_: impl Ctx, _primary: (), hue: Fraction, #[default(1.)] saturation: Fraction, #[default(0.5)] lightness: Fraction, #[default(1.)] alpha: Fraction) -> Table<Color> {
|
||||
let hue = (hue as f32) - (hue as f32).floor();
|
||||
let saturation = (saturation as f32).clamp(0., 1.);
|
||||
let lightness = (lightness as f32).clamp(0., 1.);
|
||||
let alpha = (alpha as f32).clamp(0., 1.);
|
||||
|
||||
Table::new_from_element(Color::from_hsla(hue, saturation, lightness, alpha))
|
||||
}
|
||||
|
||||
/// Constructs a color value from an sRGB color code string, such as `#RRGGBB` or `#RRGGBBAA`. Invalid hex code strings produce no color.
|
||||
#[node_macro::node(category("Color"), name("Hex to Color"))]
|
||||
fn hex_to_color(_: impl Ctx, hex_code: String) -> Table<Color> {
|
||||
match Color::from_hex_str(&hex_code) {
|
||||
Some(c) => Table::new_from_element(c),
|
||||
None => Table::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Constructs a gradient value which may be set to any sequence of color stops to represent the transition between colors.
|
||||
#[node_macro::node(category("Value"))]
|
||||
fn gradient_value(_: impl Ctx, _primary: (), gradient: Table<GradientStops>) -> Table<GradientStops> {
|
||||
|
||||
@@ -1,16 +1,11 @@
|
||||
use core_types::color::Color;
|
||||
use core_types::context::Ctx;
|
||||
use core_types::registry::types::IntegerCount;
|
||||
use core_types::table::{Table, TableRow};
|
||||
use raster_types::{CPU, Raster};
|
||||
|
||||
#[node_macro::node(category("Color"))]
|
||||
async fn image_color_palette(
|
||||
_: impl Ctx,
|
||||
image: Table<Raster<CPU>>,
|
||||
#[hard_min(1.)]
|
||||
#[soft_max(28.)]
|
||||
max_size: u32,
|
||||
) -> Table<Color> {
|
||||
async fn image_color_palette(_: impl Ctx, image: Table<Raster<CPU>>, #[default(4)] count: IntegerCount) -> Table<Color> {
|
||||
const GRID: f32 = 3.;
|
||||
|
||||
let bins = GRID * GRID * GRID;
|
||||
@@ -35,7 +30,7 @@ async fn image_color_palette(
|
||||
|
||||
shorted
|
||||
.iter()
|
||||
.take(max_size as usize)
|
||||
.take(count as usize)
|
||||
.flat_map(|&i| {
|
||||
let list = &color_bins[i];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user