Add the Image Color Palette node (#1311)

* Add image color palette node

* Add max size of palette

* Code review cleanup

---------

Co-authored-by: 0hypercube <0hypercube@gmail.com>
Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
Henry Barreto 2023-12-09 20:21:41 -03:00 committed by GitHub
parent fe4b9ef8bb
commit cbda811480
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 148 additions and 0 deletions

View file

@ -617,6 +617,26 @@ impl GraphicElementRendered for Option<Color> {
fn add_click_targets(&self, _click_targets: &mut Vec<ClickTarget>) {}
}
impl GraphicElementRendered 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.rgba_hex()));
});
}
}
fn bounding_box(&self, _transform: DAffine2) -> Option<[DVec2; 2]> {
None
}
fn add_click_targets(&self, _click_targets: &mut Vec<ClickTarget>) {}
}
/// A segment of an svg string to allow for embedding blob urls
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SvgSegment {

View file

@ -1128,4 +1128,12 @@ mod index_node {
ImageFrame::empty()
}
}
#[node_macro::node_impl(IndexNode)]
pub fn index_node(input: Vec<Color>, index: u32) -> Option<Color> {
if index as usize >= input.len() {
warn!("Index of colors is out of range: index is {index} and length is {}", input.len());
}
input.into_iter().nth(index as usize)
}
}