Skip to content

Commit 56a14b4

Browse files
committed
fix(color): Fix brightness components computation
1 parent acbb261 commit 56a14b4

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

src/color.rs

+38-3
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl RgbTransform {
6969
}
7070

7171
pub fn brightness_components(&self) -> BrightnessComponents {
72-
let fw = self.brightness_compensation as f32 + 2.0 / 100.0 + 1.0;
72+
let fw = self.brightness_compensation as f32 * 2.0 / 100.0 + 1.0;
7373
let fcmy = self.brightness_compensation as f32 / 100.0 + 1.0;
7474

7575
if self.brightness > 0 {
@@ -81,8 +81,8 @@ impl RgbTransform {
8181

8282
BrightnessComponents {
8383
rgb: (255.0 / b_in).min(255.0) as u8,
84-
cmy: (255.0 / (b_in + fcmy)).min(255.0) as u8,
85-
w: (255.0 / (b_in + fw)).min(255.0) as u8,
84+
cmy: (255.0 / (b_in * fcmy)).min(255.0) as u8,
85+
w: (255.0 / (b_in * fw)).min(255.0) as u8,
8686
}
8787
} else {
8888
BrightnessComponents::default()
@@ -359,4 +359,39 @@ impl ChannelAdjustments {
359359
}
360360
}
361361

362+
#[cfg(test)]
363+
mod tests {
364+
use super::*;
365+
366+
lazy_static::lazy_static! {
367+
static ref BASE_COLORS: [Color; 8] = [
368+
Color::new(0, 0, 0),
369+
Color::new(255, 255, 255),
370+
Color::new(255, 0, 0),
371+
Color::new(0, 255, 0),
372+
Color::new(0, 0, 255),
373+
Color::new(255, 255, 0),
374+
Color::new(0, 255, 255),
375+
Color::new(255, 0, 255),
376+
];
377+
}
378+
379+
#[test]
380+
fn test_rgb_channel_adjustment() {
381+
for &color in &*BASE_COLORS {
382+
assert_eq!(color, RgbChannelAdjustment::from(color).apply(255, 255));
383+
assert_eq!(color / 2, RgbChannelAdjustment::from(color).apply(127, 255));
384+
assert_eq!(color / 2, RgbChannelAdjustment::from(color).apply(255, 127));
385+
}
386+
}
362387

388+
#[test]
389+
fn test_color_adjustment_data() {
390+
let channel_adjustment: ColorAdjustmentData =
391+
(&crate::models::ChannelAdjustment::default()).into();
392+
393+
for &color in &*BASE_COLORS {
394+
assert_eq!(color, channel_adjustment.apply(color));
395+
}
396+
}
397+
}

0 commit comments

Comments
 (0)