color/internal/helper/gamma.go

41 lines
1.2 KiB
Go

package helper
import "math"
// Linearize converts an sRGB component in the range [0, 0xffff] to a linearRGB component in the range [0, 1].
func Linearize(c uint32) float64 {
return LinearizeF(float64(c) / 0xffff)
}
// LinearizeF converts an sRGB component in the range [0, 1] to a linearRGB component in the range [0, 1].
func LinearizeF(l float64) float64 {
if l <= 0.039285714285714285714285714285714 {
return l / 12.923210180787861094641554898407
}
return math.Pow((l+0.055)/1.055, 2.4)
}
// Delinearize converts a linearRGB component in the range [0, 1] to an sRGB component in the range [0, 0xffff].
func Delinearize(l float64) uint32 {
switch {
case l <= 0:
return 0
case l <= 0.0030399346397784299969770436366690:
return uint32(l*846922.57919793247683733430026710 + 0.5)
case l >= 1:
return 0xffff
default:
return uint32(69139.425*math.Pow(l, 1/2.4) - 3603.925)
}
}
// Delinearize converts a linearRGB component in the range [0, 1] to an sRGB component in the range [0, 1].
func DelinearizeF(l float64) float64 {
if l <= 0.0030399346397784299969770436366690 {
return l * 12.923210180787861094641554898407
}
return 1.055*math.Pow(l, 1/2.4) - 0.055
}