KCL: [number; 3] to RGB hex string color function (#7184)

Closes https://github.com/KittyCAD/modeling-app/issues/6805. Enables users to programatically construct colors, which will be helpful for 

- Applying color to visualize program execution and help debugging
- Doing weird cool shit
This commit is contained in:
Adam Chalmers
2025-05-23 13:53:58 -05:00
committed by GitHub
parent 034366e65e
commit 1f53dd1357
17 changed files with 285 additions and 3 deletions

View File

@ -10,7 +10,10 @@ use rgba_simple::Hex;
use super::args::TyF64;
use crate::{
errors::{KclError, KclErrorDetails},
execution::{types::RuntimeType, ExecState, KclValue, SolidOrImportedGeometry},
execution::{
types::{ArrayLen, RuntimeType},
ExecState, KclValue, SolidOrImportedGeometry,
},
std::Args,
};
@ -18,6 +21,34 @@ lazy_static::lazy_static! {
static ref HEX_REGEX: Regex = Regex::new(r"^#[0-9a-fA-F]{6}$").unwrap();
}
/// Construct a color from its red, blue and green components.
pub async fn hex_string(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
let rgb: [TyF64; 3] = args.get_unlabeled_kw_arg_typed(
"rgb",
&RuntimeType::Array(Box::new(RuntimeType::count()), ArrayLen::Known(3)),
exec_state,
)?;
// Make sure the color if set is valid.
if let Some(component) = rgb.iter().find(|component| component.n < 0.0 || component.n > 255.0) {
return Err(KclError::Semantic(KclErrorDetails::new(
format!("Colors are given between 0 and 255, so {} is invalid", component.n),
vec![args.source_range],
)));
}
inner_hex_string(rgb, exec_state, args).await
}
async fn inner_hex_string(rgb: [TyF64; 3], _: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
let [r, g, b] = rgb.map(|n| n.n.floor() as u32);
let s = format!("#{r:02x}{g:02x}{b:02x}");
Ok(KclValue::String {
value: s,
meta: args.into(),
})
}
/// Set the appearance of a solid. This only works on solids, not sketches or individual paths.
pub async fn appearance(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
let solids = args.get_unlabeled_kw_arg_typed(