33 lines
711 B
Go
33 lines
711 B
Go
package color
|
|
|
|
import "math"
|
|
|
|
// RGB creates a new color with red, green and blue values in the range of 0.0 to 1.0.
|
|
func RGB(r Value, g Value, b Value) Color {
|
|
return Color{r, g, b}
|
|
}
|
|
|
|
// inSRGB indicates whether the given color can be mapped to the sRGB color space.
|
|
func inSRGB(l Value, a Value, b Value, chroma Value) bool {
|
|
r, g, b := oklabToLinearRGB(l, a*chroma, b*chroma)
|
|
return r >= 0 && g >= 0 && b >= 0 && r <= 1 && g <= 1 && b <= 1
|
|
}
|
|
|
|
// sRGB performs gamma correction to convert linear RGB to sRGB.
|
|
// Source: IEC 61966-2-2
|
|
func sRGB(x Value) Value {
|
|
if x < 0 {
|
|
return 0
|
|
}
|
|
|
|
if x < 0.0031308 {
|
|
return 12.92 * x
|
|
}
|
|
|
|
if x < 1.0 {
|
|
return 1.055*math.Pow(x, 1/2.4) - 0.055
|
|
}
|
|
|
|
return 1.0
|
|
}
|