color/internal/helper/gamma.go
2025-03-06 13:01:21 -05:00

29 lines
739 B
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 {
l := float64(c) / 0xffff
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)
}
}