更正逆向计算时当颜色分量为零时的处理。

This commit is contained in:
徐涛 2025-01-14 06:08:01 +08:00
parent 2cb39adc8e
commit 665821700b

View File

@ -13,9 +13,21 @@ pub struct MixReversing {
impl MixReversing { impl MixReversing {
pub fn from_tint_rgb(basic_color: Rgb, mixed_result: Rgb) -> Self { pub fn from_tint_rgb(basic_color: Rgb, mixed_result: Rgb) -> Self {
let r_factor = (mixed_result.red - basic_color.red) / (1.0 - basic_color.red); let r_factor = if basic_color.red == 1.0 {
let g_factor = (mixed_result.green - basic_color.green) / (1.0 - basic_color.green); 0.0
let b_factor = (mixed_result.blue - basic_color.blue) / (1.0 - basic_color.blue); } else {
(mixed_result.red - basic_color.red) / (1.0 - basic_color.red)
};
let g_factor = if basic_color.green == 1.0 {
0.0
} else {
(mixed_result.green - basic_color.green) / (1.0 - basic_color.green)
};
let b_factor = if basic_color.blue == 1.0 {
0.0
} else {
(mixed_result.blue - basic_color.blue) / (1.0 - basic_color.blue)
};
let average = (r_factor + g_factor + b_factor) / 3.0; let average = (r_factor + g_factor + b_factor) / 3.0;
MixReversing { MixReversing {
@ -27,9 +39,21 @@ impl MixReversing {
} }
pub fn from_shade_rgb(basic_color: Rgb, mixed_result: Rgb) -> Self { pub fn from_shade_rgb(basic_color: Rgb, mixed_result: Rgb) -> Self {
let r_factor = (basic_color.red - mixed_result.red) / (0.0 - basic_color.red); let r_factor = if basic_color.red == 0.0 {
let g_factor = (basic_color.green - mixed_result.green) / (0.0 - basic_color.green); 0.0
let b_factor = (basic_color.blue - mixed_result.blue) / (0.0 - basic_color.blue); } else {
(basic_color.red - mixed_result.red) / (0.0 - basic_color.red)
};
let g_factor = if basic_color.green == 0.0 {
0.0
} else {
(basic_color.green - mixed_result.green) / (0.0 - basic_color.green)
};
let b_factor = if basic_color.blue == 0.0 {
0.0
} else {
(basic_color.blue - mixed_result.blue) / (0.0 - basic_color.blue)
};
let average = (r_factor + g_factor + b_factor) / 3.0; let average = (r_factor + g_factor + b_factor) / 3.0;
MixReversing { MixReversing {