mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Vibrance: Photoshop's Saturation and Vibrance, measured (linear light, 0.288 R + 0.712 G grey) (#1418)
Saturation −100 greyed to the HSL lightness, so a yellow (255, 255, 0) became grey 128 where Photoshop shows near-white: a yellow logo with Brightness/Contrast +150 and Vibrance › Saturation −100 clipped to it stayed yellow under Lighter Color. Measured black-box on Photoshop 25.4 (16-bit runs of 7,400-colour charts and HSV sweeps; sRGB, Adobe RGB and ProPhoto documents), both sliders work in linear light through the document tone curve, Vibrance first, then Saturation: - Saturation moves each colour from the grey 0.288 R + 0.712 G (blue has no weight, in every profile) by 1 + s, clipping per channel: within 0.07/255. - Negative Vibrance is an HSV change in linear light keeping the hue, in closed form: 0.2/255 on average, the deepest shadows (V < 0.05) drift. - Positive Vibrance: the brightness term is exact; the saturation boost (stronger for dull and dark colours, damped on reds and skin) is a fit, 0.4 (+10) to 5 (+100) /255 on average. CPU (compose::adjust::vibrance_px) and GPU (compose.wgsl) share the constants; the parity test gains a negative Vibrance and Saturation −100. photocraft-corpus adjustments/rgb8|rgb16/ vibrance.psd (Vibrance +60, Saturation −15): 25.6/255 mean, 89 max → 0.38 mean, 5 max. Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
1 parent
64db46f5ab
commit
4649946900
5 files changed
+225
-11
No files matched your search
@@ -143,12 +143,8 @@ pub fn apply_depth(adj: &Adjustment, buf: &mut Buffer, transfer: Transfer, depth
|
||||
}
|
||||
Adjustment::Vibrance { vibrance, saturation } => {
|
||||
let (v, s) = (*vibrance / 100.0, *saturation / 100.0);
|
||||
map_rgb(buf, |c| {
|
||||
let (h, sat, l) = rgb_to_hsl(c);
|
||||
let boost = v * (1.0 - sat); // less saturated colours move more
|
||||
let ns = (sat * (1.0 + s) + boost * sat.max(0.1)).clamp(0.0, 1.0);
|
||||
hsl_to_rgb(h, ns, l)
|
||||
})
|
||||
let hi = if depth == Some(SampleType::F32) { f32::INFINITY } else { 1.0 };
|
||||
map_rgb(buf, |c| vibrance_px(c, v, s, transfer, hi))
|
||||
}
|
||||
Adjustment::ChannelMixer { matrix, monochrome } => map_rgb(buf, |c| {
|
||||
let mix = |row: &[f32; 4]| (row[0] * c[0] + row[1] * c[1] + row[2] * c[2] + row[3]).clamp(0.0, 1.0);
|
||||
@@ -479,6 +475,90 @@ pub fn photo_filter_matrix(color: [f32; 3], density: f32, normalize_y: bool) ->
|
||||
std::array::from_fn(|r| std::array::from_fn(|c| (0..3).map(|k| XYZ_D50_TO_SRGB[r][k] * s[k] * SRGB_TO_XYZ_D50[k][c]).sum()))
|
||||
}
|
||||
|
||||
/// Photoshop's Vibrance on one pixel (`v`, `s`: the sliders / 100; `hi`: the sample ceiling).
|
||||
///
|
||||
/// Measured black-box against Photoshop 25.4 on 7,400-colour charts (8- and 16-bit; sRGB, Adobe RGB
|
||||
/// and ProPhoto documents). Both sliders work in linear light through the document's tone curve,
|
||||
/// Vibrance first, then Saturation:
|
||||
/// - Saturation moves each colour away from (or towards) the grey `0.288 R + 0.712 G` (blue has no
|
||||
/// weight, in every profile) by `1 + s`, clipping each channel: within 0.07/255 at every `s`.
|
||||
/// - Vibrance is an HSV change in linear light that keeps the hue: see [`vibrance_lin`].
|
||||
pub fn vibrance_px(c: [f32; 3], v: f32, s: f32, transfer: Transfer, hi: f32) -> [f32; 3] {
|
||||
let mut l = c.map(|x| transfer.decode(x));
|
||||
if v != 0.0 {
|
||||
l = vibrance_lin(l, v);
|
||||
}
|
||||
if s != 0.0 {
|
||||
let g = VIBRANCE_GREY[0] * l[0] + VIBRANCE_GREY[1] * l[1];
|
||||
l = l.map(|x| (g + (1.0 + s) * (x - g)).clamp(0.0, hi));
|
||||
}
|
||||
l.map(|x| transfer.encode(x.clamp(0.0, hi)))
|
||||
}
|
||||
|
||||
/// Weights of the grey Photoshop's Vibrance › Saturation desaturates to (R, G; blue has none).
|
||||
pub const VIBRANCE_GREY: [f32; 2] = [0.288, 0.712];
|
||||
|
||||
/// Positive Vibrance's saturation boost, fitted to the measurements (see [`vibrance_lin`]).
|
||||
pub const VIBRANCE_BOOST: [f32; 7] = [0.5001, 0.0887, 0.4374, 0.7573, -0.8113, 0.5154, 2.8026];
|
||||
|
||||
/// The Vibrance slider (`v` in [-1, 1]) on a linear-light pixel. With `t = 1 - min/max` (HSV
|
||||
/// saturation) and `V = max`:
|
||||
/// - below 0 (`a = -v`): `t' = t (1 - a/4) (1 - a + a t (1 + t) / 2)` and
|
||||
/// `V' = V - a t (1 - t) (2 - t + t²) V (1 - V)`; exact (0.2/255 mean) except in the deepest
|
||||
/// shadows (`V` under 0.05), which Photoshop desaturates less.
|
||||
/// - above 0: `V' = V + v/4 t (1 - t) (2 - t + t²) V (1 - V)` (exact) and a saturation boost that
|
||||
/// grows as `t` and `V` fall, damped on reds and skin (HSV hue 0–30°, fading out by 45° and in
|
||||
/// from 300°): a fit, 0.4 (+10) to 5 (+100) /255 off on average.
|
||||
pub fn vibrance_lin(c: [f32; 3], v: f32) -> [f32; 3] {
|
||||
let mx = c[0].max(c[1]).max(c[2]);
|
||||
let mn = c[0].min(c[1]).min(c[2]);
|
||||
if mx <= 0.0 || mx - mn <= 1e-9 {
|
||||
return c;
|
||||
}
|
||||
let t = 1.0 - mn / mx;
|
||||
let shape = t * (1.0 - t) * (2.0 - t + t * t) * mx * (1.0 - mx).max(0.0);
|
||||
let (t2, v2) = if v < 0.0 {
|
||||
let a = -v;
|
||||
(t * (1.0 - a / 4.0) * (1.0 - a + a * t * (1.0 + t) / 2.0), mx - a * shape)
|
||||
} else {
|
||||
let [c0, c1, p, q, r0, ra, rt] = VIBRANCE_BOOST;
|
||||
let w = vibrance_skin(hsv_hue(c, mx, mn));
|
||||
let a = v * (1.0 - w) + 0.64 * v.powf(1.6) * w;
|
||||
let boost = (c0 * a + c1 * a * a) * t.powf(p) * (1.0 - t).powf(q) * ((r0 + ra * a + rt * (1.0 - t)) * (1.0 - mx).max(0.0)).exp();
|
||||
((t * (1.0 + boost)).min(1.0), mx + v / 4.0 * shape)
|
||||
};
|
||||
let m2 = v2 * (1.0 - t2);
|
||||
let k = (v2 - m2) / (mx - mn);
|
||||
c.map(|x| m2 + (x - mn) * k)
|
||||
}
|
||||
|
||||
/// How much positive Vibrance treats a hue (degrees) as skin: 1 on 0–30°, fading to 0 by 45° and
|
||||
/// in again from 300°.
|
||||
fn vibrance_skin(h: f32) -> f32 {
|
||||
if h <= 30.0 {
|
||||
1.0
|
||||
} else if h < 45.0 {
|
||||
(45.0 - h) / 15.0
|
||||
} else if h < 300.0 {
|
||||
0.0
|
||||
} else {
|
||||
(h - 300.0) / 60.0
|
||||
}
|
||||
}
|
||||
|
||||
/// HSV hue in degrees of `c` (its max `mx` and min `mn` given, `mx > mn`).
|
||||
fn hsv_hue(c: [f32; 3], mx: f32, mn: f32) -> f32 {
|
||||
let d = mx - mn;
|
||||
let h = if mx == c[0] {
|
||||
((c[1] - c[2]) / d).rem_euclid(6.0)
|
||||
} else if mx == c[1] {
|
||||
(c[2] - c[0]) / d + 2.0
|
||||
} else {
|
||||
(c[0] - c[1]) / d + 4.0
|
||||
};
|
||||
h * 60.0
|
||||
}
|
||||
|
||||
/// Photoshop posterize: `n` equal input bins over 0..=255, output levels
|
||||
/// `floor(k * 255 / (n - 1))` (exact on the corpus for 3, 7, 13 and 21 levels).
|
||||
/// Modern Brightness curve (one channel, `brightness` in [-150, 150]). A line of slope
|
||||
|
||||
@@ -1565,3 +1565,88 @@ fn photo_filter_matches_photoshop() {
|
||||
assert!(got.iter().zip(ps).all(|(g, p)| (g - p).abs() <= 2.5), "F32 {v:?}: got {got:?} want {ps:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Vibrance of an 8-bit sRGB colour (0–255 in and out).
|
||||
fn vibrance_255(c: [f32; 3], vibrance: f32, saturation: f32) -> [f32; 3] {
|
||||
let mut buf = Buffer::filled(Rect::new(0, 0, 1, 1), [c[0] / 255.0, c[1] / 255.0, c[2] / 255.0, 1.0]);
|
||||
adjust::apply(&Adjustment::Vibrance { vibrance, saturation }, &mut buf);
|
||||
let p = buf.px[0];
|
||||
[p[0] * 255.0, p[1] * 255.0, p[2] * 255.0]
|
||||
}
|
||||
|
||||
fn assert_near_255(got: [f32; 3], want: [f32; 3], tol: f32, what: &str) {
|
||||
assert!(got.iter().zip(want).all(|(g, w)| (g - w).abs() <= tol), "{what}: got {got:?}, Photoshop {want:?}");
|
||||
}
|
||||
|
||||
// Photoshop 25.4 references (16-bit runs of 8-bit colours in an sRGB document).
|
||||
|
||||
#[test]
|
||||
fn vibrance_saturation_matches_photoshop() {
|
||||
// −100 greys to 0.288 R + 0.712 G in linear light: blue has no weight, yellow stays bright.
|
||||
for (c, g) in [([251.0, 201.0, 0.0], 217.0), ([255.0, 0.0, 0.0], 146.0), ([0.0, 255.0, 0.0], 219.4), ([0.0, 0.0, 255.0], 0.0), ([255.0, 255.0, 0.0], 255.0)]
|
||||
{
|
||||
assert_near_255(vibrance_255(c, 0.0, -100.0), [g; 3], 0.5, "saturation -100");
|
||||
}
|
||||
for (c, s, want) in [
|
||||
([255.0, 128.0, 0.0], -50.0, [220.7, 155.2, 129.4]),
|
||||
([224.0, 176.0, 144.0], -50.0, [208.6, 183.9, 169.9]),
|
||||
([64.0, 128.0, 192.0], 50.0, [0.0, 134.3, 218.7]),
|
||||
([224.0, 176.0, 144.0], 100.0, [251.3, 158.5, 51.7]),
|
||||
([32.0, 96.0, 64.0], 100.0, [0.0, 106.8, 32.8]),
|
||||
] {
|
||||
assert_near_255(vibrance_255(c, 0.0, s), want, 0.5, "saturation");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn negative_vibrance_matches_photoshop() {
|
||||
for (c, v, want) in [
|
||||
([255.0, 0.0, 0.0], -100.0, [255.0, 137.0, 137.0]),
|
||||
([128.0, 0.0, 0.0], -100.0, [128.0, 65.7, 65.7]),
|
||||
([224.0, 176.0, 144.0], -100.0, [213.2, 197.4, 188.8]),
|
||||
([64.0, 128.0, 192.0], -100.0, [124.4, 149.0, 185.0]),
|
||||
([255.0, 128.0, 0.0], -50.0, [255.0, 152.0, 99.1]),
|
||||
([251.0, 201.0, 0.0], -50.0, [251.0, 208.1, 97.4]),
|
||||
([32.0, 96.0, 64.0], -50.0, [50.1, 91.7, 68.6]),
|
||||
] {
|
||||
assert_near_255(vibrance_255(c, v, 0.0), want, 0.6, "vibrance");
|
||||
}
|
||||
// Vibrance applies first, then Saturation.
|
||||
assert_near_255(vibrance_255([64.0, 128.0, 192.0], 50.0, -50.0), [86.5, 117.8, 158.4], 3.0, "vibrance then saturation");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn positive_vibrance_follows_photoshop_closely() {
|
||||
// A fit, not exact: within a few levels, saturated colours untouched, skin damped.
|
||||
for (c, v, want, tol) in [
|
||||
([255.0, 128.0, 0.0], 50.0, [255.0, 128.0, 0.0], 0.5),
|
||||
([144.0, 160.0, 176.0], 100.0, [121.2, 152.6, 180.5], 1.5),
|
||||
([64.0, 128.0, 192.0], 50.0, [50.6, 125.1, 192.8], 4.0),
|
||||
([224.0, 176.0, 144.0], 50.0, [224.9, 173.4, 138.0], 3.0),
|
||||
([224.0, 176.0, 144.0], 100.0, [226.6, 166.8, 122.4], 5.0),
|
||||
] {
|
||||
assert_near_255(vibrance_255(c, v, 0.0), want, tol, "vibrance");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clipped_brightness_and_desaturation_whiten_a_lighter_color_logo() {
|
||||
// постер.psd: a yellow logo in Lighter Color over the same yellow, with Brightness/Contrast +150
|
||||
// and Vibrance › Saturation −100 clipped to it. Photoshop shows it white (254); 0.5.0 left it
|
||||
// yellow (its Saturation −100 greyed (255, 255, 0) to 128, darker than the yellow beneath).
|
||||
let yellow = [251.0 / 255.0, 201.0 / 255.0, 0.0, 1.0];
|
||||
let mut d = doc_white(2, 1);
|
||||
d.layers[0].surface_mut().unwrap().fill_rect(Rect::new(0, 0, 2, 1), &yellow);
|
||||
let mut logo = solid_layer("logo", Rect::new(0, 0, 1, 1), yellow);
|
||||
logo.blend = BlendMode::LighterColor;
|
||||
d.layers.push(logo);
|
||||
for adj in [Adjustment::BrightnessContrast { brightness: 150.0, contrast: 0.0, legacy: false }, Adjustment::Vibrance { vibrance: 0.0, saturation: -100.0 }]
|
||||
{
|
||||
let mut l = Layer::new("adj", LayerContent::Adjustment(adj));
|
||||
l.clipped = true;
|
||||
d.layers.push(l);
|
||||
}
|
||||
let p = px(&d, 0, 0);
|
||||
assert!(p[..3].iter().all(|v| *v * 255.0 >= 252.0), "logo whitened: {p:?}");
|
||||
assert!(close4(px(&d, 1, 0), yellow), "the yellow beside it is untouched");
|
||||
}
|
||||
@@ -324,6 +324,42 @@ fn t_encode(v: f32, g: f32) -> f32 {
|
||||
return pow(max(v, 0.0), 1.0 / g);
|
||||
}
|
||||
|
||||
// compose::adjust::vibrance_lin: Photoshop's Vibrance slider (v in [-1, 1]) as an HSV change in
|
||||
// linear light that keeps the hue; b0 = boost fit (c0, c1, p, q), b1 = (r0, ra, rt).
|
||||
fn vibrance_lin(c: vec3<f32>, v: f32, b0: vec4<f32>, b1: vec3<f32>) -> vec3<f32> {
|
||||
let mx = max(max(c.r, c.g), c.b);
|
||||
let mn = min(min(c.r, c.g), c.b);
|
||||
if (mx <= 0.0 || mx - mn <= 1e-9) { return c; }
|
||||
let t = 1.0 - mn / mx;
|
||||
let shape = t * (1.0 - t) * (2.0 - t + t * t) * mx * max(1.0 - mx, 0.0);
|
||||
var t2: f32;
|
||||
var v2: f32;
|
||||
if (v < 0.0) {
|
||||
let a = -v;
|
||||
t2 = t * (1.0 - a / 4.0) * (1.0 - a + a * t * (1.0 + t) / 2.0);
|
||||
v2 = mx - a * shape;
|
||||
} else {
|
||||
let d = mx - mn;
|
||||
var h: f32;
|
||||
if (mx == c.r) { h = rem_euclid((c.g - c.b) / d, 6.0); }
|
||||
else if (mx == c.g) { h = (c.b - c.r) / d + 2.0; }
|
||||
else { h = (c.r - c.g) / d + 4.0; }
|
||||
h = h * 60.0;
|
||||
var w = 0.0;
|
||||
if (h <= 30.0) { w = 1.0; }
|
||||
else if (h < 45.0) { w = (45.0 - h) / 15.0; }
|
||||
else if (h >= 300.0) { w = (h - 300.0) / 60.0; }
|
||||
let a = v * (1.0 - w) + 0.64 * pow(v, 1.6) * w;
|
||||
let boost = (b0.x * a + b0.y * a * a) * pow(t, b0.z) * pow(1.0 - t, b0.w)
|
||||
* exp((b1.x + b1.y * a + b1.z * (1.0 - t)) * max(1.0 - mx, 0.0));
|
||||
t2 = min(t * (1.0 + boost), 1.0);
|
||||
v2 = mx + v / 4.0 * shape;
|
||||
}
|
||||
let m2 = v2 * (1.0 - t2);
|
||||
let k = (v2 - m2) / (mx - mn);
|
||||
return vec3(m2) + (c - vec3(mn)) * k;
|
||||
}
|
||||
|
||||
fn rgb_to_hsl(c: vec3<f32>) -> vec3<f32> {
|
||||
let mx = max(max(c.r, c.g), c.b);
|
||||
let mn = min(min(c.r, c.g), c.b);
|
||||
@@ -522,10 +558,16 @@ fn adjust(c: vec3<f32>) -> vec3<f32> {
|
||||
return rgb;
|
||||
}
|
||||
case 9: { // Vibrance
|
||||
let hsl = rgb_to_hsl(c);
|
||||
let boost = p0.x * (1.0 - hsl.y);
|
||||
let ns = clamp(hsl.y * (1.0 + p0.y) + boost * max(hsl.y, 0.1), 0.0, 1.0);
|
||||
return hsl_to_rgb(hsl.x, ns, hsl.z);
|
||||
// compose::adjust::vibrance_px: linear light (transfer p0.z), Vibrance (p0.x, boost
|
||||
// fit p1, p2.xyz) then Saturation (p0.y) towards 0.288 R + 0.712 G; p0.w the ceiling.
|
||||
var l = vec3(t_decode(c.r, p0.z), t_decode(c.g, p0.z), t_decode(c.b, p0.z));
|
||||
if (p0.x != 0.0) { l = vibrance_lin(l, p0.x, p1, p2.xyz); }
|
||||
if (p0.y != 0.0) {
|
||||
let g = 0.288 * l.r + 0.712 * l.g;
|
||||
l = clamp(vec3(g) + (1.0 + p0.y) * (l - vec3(g)), vec3(0.0), vec3(p0.w));
|
||||
}
|
||||
l = clamp(l, vec3(0.0), vec3(p0.w));
|
||||
return vec3(t_encode(l.r, p0.z), t_encode(l.g, p0.z), t_encode(l.b, p0.z));
|
||||
}
|
||||
case 10: { // Channel mixer
|
||||
let r = clamp(dot(p0.xyz, c) + p0.w, 0.0, 1.0);
|
||||
|
||||
@@ -1487,7 +1487,12 @@ pub fn adjustment_program(adj: &Adjustment, transfer: Transfer, depth: photocraf
|
||||
}
|
||||
}
|
||||
Adjustment::Vibrance { vibrance, saturation } => {
|
||||
p[0] = [vibrance / 100.0, saturation / 100.0, 0.0, 0.0];
|
||||
// compose::adjust::vibrance_px; 32-bit samples aren't clipped at 1.
|
||||
let hi = if depth == photocraft_color::SampleType::F32 { f32::MAX } else { 1.0 };
|
||||
p[0] = [vibrance / 100.0, saturation / 100.0, transfer_exponent(transfer), hi];
|
||||
let b = adjust::VIBRANCE_BOOST;
|
||||
p[1] = [b[0], b[1], b[2], b[3]];
|
||||
p[2] = [b[4], b[5], b[6], 0.0];
|
||||
(9, p, None)
|
||||
}
|
||||
Adjustment::ChannelMixer { matrix, monochrome } => {
|
||||
|
||||
@@ -200,6 +200,8 @@ fn adjustments() -> Vec<Adjustment> {
|
||||
Adjustment::HueSaturation { hue: 200.0, saturation: 50.0, lightness: 20.0, colorize: true, ranges: HueRange::defaults() },
|
||||
Adjustment::HueSaturation { hue: -10.0, saturation: 10.0, lightness: 5.0, colorize: false, ranges: hue_ranges() },
|
||||
Adjustment::Vibrance { vibrance: 50.0, saturation: -20.0 },
|
||||
Adjustment::Vibrance { vibrance: -60.0, saturation: 0.0 },
|
||||
Adjustment::Vibrance { vibrance: 0.0, saturation: -100.0 },
|
||||
Adjustment::ChannelMixer { matrix: [[0.5, 0.3, 0.2, 0.0], [0.1, 0.8, 0.1, 0.05], [0.0, 0.2, 0.9, -0.05]], monochrome: false },
|
||||
Adjustment::ChannelMixer { matrix: [[0.4, 0.4, 0.2, 0.0], [0.0; 4], [0.0; 4]], monochrome: true },
|
||||
Adjustment::PhotoFilter { color: [0.9, 0.6, 0.2], density: 0.4, preserve_luminosity: true },
|
||||
|
||||
Reference in new issue
Block a user