Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- impl Color {
- pub fn rgb(& self) -> (u8, u8, u8) {
- match *self {
- Color::RGB{r, g, b} => (r, g, b),
- Color::CMYK{c, m, y, k} => {
- ((255.0 * (1.0 - c) * (1.0 - k)) as u8,
- (255.0 * (1.0 - m) * (1.0 - k)) as u8,
- (255.0 * (1.0 - y) * (1.0 - k)) as u8)
- }
- Color::HSL{h, s, l} => {
- // Saturation == 0
- if f64::abs(s) < 1e-10 {
- return ((255.0 * l) as u8,
- (255.0 * l) as u8,
- (255.0 * l) as u8);
- }
- let v2: f64;
- let v1: f64;
- if l < 0.5 {
- v2 = l * (1.0 + s);
- } else {
- v2 = (l + s) - s * l
- }
- v1 = 2.0 * l - v2;
- ((255.0 * Self::hue2rgb(
- v1, v2, ((h as f64)/360.0 + 1.0/3.0))) as u8,
- (255.0 * Self::hue2rgb(
- v1, v2, (h as f64)/360.0)) as u8,
- (255.0 * Self::hue2rgb(
- v1, v2, ((h as f64)/360.0 - 1.0/3.0))) as u8)
- }
- }
- }
- }
- impl Color {
- fn hue2rgb(v1: f64, v2: f64, h: f64) -> f64 {
- let mut _h = h;
- if _h < 0.0 {
- _h += 1.0;
- }
- if _h > 1.0 {
- _h -= 1.0;
- }
- if 6.0 * _h < 1.0 {
- return v1 + (v2 - v1) * 6.0 * _h;
- }
- if 2.0 * _h < 1.0 {
- return v2;
- }
- if 3.0 * _h < 2.0 {
- return v1 + (v2 - v1) * ((2.0 / 3.0 - _h) * 6.0);
- }
- v1
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement