Created
May 31, 2024 02:09
-
-
Save kujirahand/cefdd221f49c0de2af97ab0747e49583 to your computer and use it in GitHub Desktop.
RustでHSLからRGBに変換する関数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn hsl_to_rgb(h: f64, s: f64, l: f64) -> (u8, u8, u8) { | |
let (mut r, mut g, mut b) = (l, l, l); | |
if s != 0.0 { | |
let hue2rgb = |p: f64, q: f64, t: f64| -> f64 { | |
let mut t = t; | |
if t < 0.0 { t += 1.0; } | |
if t > 1.0 { t -= 1.0; } | |
if t < 1.0 / 6.0 { return p + (q - p) * 6.0 * t; } | |
if t < 1.0 / 2.0 { return q; } | |
if t < 2.0 / 3.0 { return p + (q - p) * (2.0 / 3.0 - t) * 6.0; } | |
p | |
}; | |
let q = if l < 0.5 { l * (1.0 + s) } else { l + s - l * s }; | |
let p = 2.0 * l - q; | |
r = hue2rgb(p, q, h + 1.0 / 3.0); | |
g = hue2rgb(p, q, h); | |
b = hue2rgb(p, q, h - 1.0 / 3.0); | |
} | |
((r * 255.0).round() as u8, (g * 255.0).round() as u8, (b * 255.0).round() as u8) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment