Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Add built-in rainbow gradient presets (#2916)

Myosh13f0970923d6b
diff --git a/crates/engine/src/presets/gradients.rs b/crates/engine/src/presets/gradients.rs
index 475f3b3f6..7c92dfba9 100644
--- a/crates/engine/src/presets/gradients.rs
+++ b/crates/engine/src/presets/gradients.rs
@@ -234,6 +234,13 @@ pub fn builtin() -> Vec<Group<GradientPreset>> {
stops: vec![(0.0, StopColor::Foreground), (1.0, StopColor::Foreground)],
opacity: vec![(0.0, 1.0), (1.0, 0.0)],
};
+ // A smooth spectrum: 13 hues evenly spaced from red (0°) to magenta (300°). The pastel and
+ // neon presets reuse the same hues at a different saturation/lightness, and Rainbow Fade
+ // reuses the classic ramp with its ends transparent. Colours are our own.
+ let rainbow = GradientPreset::new(
+ "Rainbow",
+ &["#ec1313", "#ec6d13", "#ecc813", "#b6ec13", "#5bec13", "#13ec25", "#13ec80", "#13ecda", "#13a4ec", "#1349ec", "#3713ec", "#9213ec", "#ec13ec"],
+ );
vec![
Group::new("Basics", vec![GradientPreset::foreground_to_background(), fg_transparent, GradientPreset::new("Black, White", &["#000000", "#ffffff"])]),
Group::new(
@@ -348,6 +355,31 @@ pub fn builtin() -> Vec<Group<GradientPreset>> {
],
),
),
+ Group::new(
+ "Rainbows",
+ vec![
+ rainbow.clone(),
+ GradientPreset::new(
+ "Pastel Rainbow",
+ &[
+ "#f4afaf", "#f4cbaf", "#f4e8af", "#e2f4af", "#c6f4af", "#aff4b4", "#aff4d1", "#aff4ee", "#afddf4", "#afc0f4", "#baaff4", "#d7aff4",
+ "#f4aff4",
+ ],
+ ),
+ GradientPreset::new(
+ "Neon Rainbow",
+ &[
+ "#ff0000", "#ff6a00", "#ffd400", "#bfff00", "#55ff00", "#00ff15", "#00ff80", "#00ffea", "#00aaff", "#0040ff", "#2a00ff", "#9500ff",
+ "#ff00ff",
+ ],
+ ),
+ GradientPreset::new(
+ "Oil Slick",
+ &["#0b0f1a", "#1b2a4a", "#0f6f6f", "#1fae8a", "#5ac8a8", "#7a5cff", "#b13bd6", "#e04a8f", "#f2a65a", "#3a2a6b", "#0b0f1a"],
+ ),
+ GradientPreset { name: "Rainbow Fade".into(), opacity: vec![(0.0, 0.0), (0.125, 1.0), (0.875, 1.0), (1.0, 0.0)], ..rainbow },
+ ],
+ ),
]
}
diff --git a/crates/engine/src/presets/tests.rs b/crates/engine/src/presets/tests.rs
index 5f36e4278..7ebf2145b 100644
--- a/crates/engine/src/presets/tests.rs
+++ b/crates/engine/src/presets/tests.rs
@@ -60,6 +60,32 @@ fn gradient_builtins_and_listing() {
assert_eq!(groups[0]["presets"][1]["transparency"][1], json!([1.0, 0.0]));
}
+#[test]
+fn rainbow_gradient_presets() {
+ let mut s = session(8);
+ let v = s.execute("gradient.presets.list", json!({})).unwrap();
+ let group = v["groups"].as_array().unwrap().iter().find(|g| g["name"] == "Rainbows").unwrap();
+ let presets = group["presets"].as_array().unwrap();
+ let names: Vec<&str> = presets.iter().map(|p| p["name"].as_str().unwrap()).collect();
+ assert_eq!(names, ["Rainbow", "Pastel Rainbow", "Neon Rainbow", "Oil Slick", "Rainbow Fade"]);
+ // The three hue ramps share the same 13 evenly spaced hues: smooth, not the 7 coarse ROYGBIV points.
+ for p in [&presets[0], &presets[1], &presets[2]] {
+ let stops = p["stops"].as_array().unwrap();
+ assert_eq!(stops.len(), 13, "{}", p["name"]);
+ assert_eq!(stops[0][0], json!(0.0));
+ assert_eq!(stops[12][0], json!(1.0));
+ }
+ assert_eq!(presets[3]["stops"].as_array().unwrap().len(), 11, "oil slick");
+ assert_eq!(presets[4]["transparency"], json!([[0.0, 0.0], [0.125, 100.0], [0.875, 100.0], [1.0, 0.0]]));
+ // Rainbow Fade paints opaque across the middle and transparent at both ends.
+ s.execute("gradient.presets.select", json!({"preset": "Rainbow Fade"})).unwrap();
+ s.execute("layer.new.layer", json!({})).unwrap();
+ s.execute("paint.gradient", json!({"from": [0, 0], "to": [63, 0]})).unwrap();
+ assert!(layer_px(&s, 0, 5)[3] < 0.1, "left edge transparent: {:?}", layer_px(&s, 0, 5));
+ assert!(layer_px(&s, 63, 5)[3] < 0.1, "right edge transparent: {:?}", layer_px(&s, 63, 5));
+ assert!(layer_px(&s, 32, 5)[3] > 0.95, "middle opaque: {:?}", layer_px(&s, 32, 5));
+}
+
#[test]
fn gradient_tool_uses_selected_preset_at_every_depth() {
for depth in [8, 16, 32] {