Repository: GraphiteEditor/Graphite
File: node-graph/libraries/rendering/src/renderer.rs (Rust)

Code before:

	transform.to_cols_array().iter().enumerate().fold("matrix(".to_string(), |val, (i, num)| {
		let num = if num.abs() < 1_000_000_000. { (num * 1_000_000_000.).round() / 1_000_000_000. } else { *num };
		let num = if num.is_zero() { "0".to_string() } else { num.to_string() };
		let comma = if i == 5 { "" } else { "," };
		val + &(num + comma)
	}) + ")"
}

fn max_scale(transform: DAffine2) -> f64 {
	let sx = transform.x_axis.length_squared();
	let sy = transform.y_axis.length_squared();
	(sx + sy).sqrt()
}

pub fn contrasting_outline_color(background: Option<Color>) -> Color {
	use core_types::consts::LAYER_OUTLINE_STROKE_COLOR;

	if let Some(bg) = background {
		let alpha = bg.a();

		// Convert Linear Float to sRGB Float
		let linear_to_srgb = |x: f32| -> f32 { if x <= 0.0031308 { x * 12.92 } else { 1.055 * x.powf(1.0 / 2.4) - 0.055 } };

		// Convert sRGB Float to Linear Float
		let srgb_to_linear = |x: f32| -> f32 { if x <= 0.04045 { x / 12.92 } else { ((x + 0.055) / 1.055).powf(2.4) } };

		let (r_srgb, g_srgb, b_srgb) = if alpha.abs() > f32::EPSILON {
			let r_lin = bg.r() / alpha;
			let g_lin = bg.g() / alpha;
			let b_lin = bg.b() / alpha;
			(linear_to_srgb(r_lin), linear_to_srgb(g_lin), linear_to_srgb(b_lin))
		} else {
			(0., 0., 0.)
		};

		let r_comp_srgb = r_srgb * alpha;
		let g_comp_srgb = g_srgb * alpha;
		let b_comp_srgb = b_srgb * alpha;

		let r_final = srgb_to_linear(r_comp_srgb);
		let g_final = srgb_to_linear(g_comp_srgb);
		let b_final = srgb_to_linear(b_comp_srgb);

		let luminance = 0.2126 * r_final + 0.7152 * g_final + 0.0722 * b_final;

		let threshold = (1.05 * 0.05f32).sqrt() - 0.05;

		if luminance > threshold { Color::BLACK } else { Color::WHITE }
	} else {
		LAYER_OUTLINE_STROKE_COLOR

Review comment (refactor):
Search the code base for `0.04045` or `12.92` and see if you can find any reusable Rust functions that are already able to do this logic for you without having to duplicate it, as well as the logic above and below this.

Code after:

	transform.to_cols_array().iter().enumerate().fold("matrix(".to_string(), |val, (i, num)| {
		let num = if num.abs() < 1_000_000_000. { (num * 1_000_000_000.).round() / 1_000_000_000. } else { *num };
		let num = if num.is_zero() { "0".to_string() } else { num.to_string() };
		let comma = if i == 5 { "" } else { "," };
		val + &(num + comma)
	}) + ")"
}

fn max_scale(transform: DAffine2) -> f64 {
	let sx = transform.x_axis.length_squared();
	let sy = transform.y_axis.length_squared();
	(sx + sy).sqrt()
}

pub fn black_or_white_for_best_contrast(background: Option<Color>) -> Color {
	let Some(bg) = background else { return core_types::consts::LAYER_OUTLINE_STROKE_COLOR };

	let alpha = bg.a();

	// Un-premultiply, then convert to gamma sRGB
	let srgb = if alpha > f32::EPSILON {
		Color::from_rgbaf32_unchecked(bg.r() / alpha, bg.g() / alpha, bg.b() / alpha, alpha).to_gamma_srgb()
	} else {
		Color::TRANSPARENT
	};

	// Composite over black in sRGB space, then convert back to linear for luminance
	let composited = Color::from_rgbaf32_unchecked(srgb.r() * alpha, srgb.g() * alpha, srgb.b() * alpha, 1.).to_linear_srgb();

	let threshold = (1.05 * 0.05f32).sqrt() - 0.05;

	if composited.luminance_srgb() > threshold { Color::BLACK } else { Color::WHITE }
}

pub fn to_transform(transform: DAffine2) -> usvg::Transform {
	let cols = transform.to_cols_array();
	usvg::Transform::from_row(cols[0] as f32, cols[1] as f32, cols[2] as f32, cols[3] as f32, cols[4] as f32, cols[5] as f32)
}

// TODO: Click targets can be removed from the render output, since the vector data is available in the vector modify data from Monitor nodes.
// This will require that the transform for child layers into that layer space be calculated, or it could be returned from the RenderOutput instead of click targets.
#[derive(Debug, Default, Clone, PartialEq, DynAny, serde::Serialize, serde::Deserialize)]
pub struct RenderMetadata {
	pub upstream_footprints: HashMap<NodeId, Footprint>,
	pub local_transforms: HashMap<NodeId, DAffine2>,
	pub first_element_source_id: HashMap<NodeId, Option<NodeId>>,
	pub click_targets: HashMap<NodeId, Vec<Arc<ClickTarget>>>,
	pub clip_targets: HashSet<NodeId>,
	pub vector_data: HashMap<NodeId, Arc<Vector>>,
}