30 lines
666 B
Go

package color
import "math"
// HSL represents a color using hue, saturation and lightness.
func HSL(hue Value, saturation Value, lightness Value) Color {
hue = math.Mod(hue, 360)
c := (1 - math.Abs(2*lightness-1)) * saturation
x := c * (1 - math.Abs(math.Mod(hue/60, 2)-1))
var r, g, b Value
switch {
case hue >= 0 && hue < 60:
r, g, b = c, x, 0
case hue >= 60 && hue < 120:
r, g, b = x, c, 0
case hue >= 120 && hue < 180:
r, g, b = 0, c, x
case hue >= 180 && hue < 240:
r, g, b = 0, x, c
case hue >= 240 && hue < 300:
r, g, b = x, 0, c
case hue >= 300 && hue < 360:
r, g, b = c, 0, x
}
m := lightness - c/2
return RGB(r+m, g+m, b+m)
}