Skip to content

Commit acbb261

Browse files
committed
refactor(color): Move color_to8 and color_to16 to utils
1 parent f648ed8 commit acbb261

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/color.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ use slotmap::{DefaultKey, SlotMap};
44

55
use crate::models::{Color, Color16};
66

7+
mod utils;
8+
pub use utils::{color_to16, color_to8};
9+
710
#[derive(Default, Debug, Clone, Copy)]
811
struct RgbChannelAdjustment {
912
adjust: Color,
@@ -356,12 +359,4 @@ impl ChannelAdjustments {
356359
}
357360
}
358361

359-
pub fn color_to8(color: Color16) -> Color {
360-
let (r, g, b) = color.into_components();
361-
Color::from_components(((r >> 8) as u8, (g >> 8) as u8, (b >> 8) as u8))
362-
}
363362

364-
pub fn color_to16(color: Color) -> Color16 {
365-
let (r, g, b) = color.into_components();
366-
Color16::from_components(((r as u16) << 8, (g as u16) << 8, (b as u16) << 8))
367-
}

src/color/utils.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//! Color utilities
2+
3+
use palette::LinSrgb;
4+
5+
use crate::models::{Color, Color16};
6+
7+
const FACTOR: u16 = 65535 / 255;
8+
9+
pub fn color_to8(color: Color16) -> Color {
10+
let (r, g, b) = color.into_components();
11+
Color::new((r / FACTOR) as u8, (g / FACTOR) as u8, (b / FACTOR) as u8)
12+
}
13+
14+
pub fn color_to16(color: Color) -> Color16 {
15+
let (r, g, b) = color.into_components();
16+
Color16::new(
17+
(r as u16) * FACTOR,
18+
(g as u16) * FACTOR,
19+
(b as u16) * FACTOR,
20+
)
21+
}

0 commit comments

Comments
 (0)