ebiten/graphics/matrix/color.go

73 lines
1.3 KiB
Go
Raw Normal View History

2013-06-20 18:47:39 +02:00
package matrix
2013-12-08 11:38:22 +01:00
import (
"image/color"
"math"
)
2013-06-20 18:47:39 +02:00
const colorDim = 5
type Color struct {
Elements [colorDim - 1][colorDim]float64
}
func IdentityColor() Color {
return Color{
[colorDim - 1][colorDim]float64{
{1, 0, 0, 0, 0},
{0, 1, 0, 0, 0},
{0, 0, 1, 0, 0},
{0, 0, 0, 1, 0},
},
}
}
func (matrix *Color) Dim() int {
return colorDim
}
func (matrix *Color) Concat(other Color) {
result := Color{}
mul(&other, matrix, &result)
*matrix = result
}
func (matrix *Color) IsIdentity() bool {
return isIdentity(matrix)
}
func (matrix *Color) element(i, j int) float64 {
return matrix.Elements[i][j]
}
func (matrix *Color) setElement(i, j int, element float64) {
matrix.Elements[i][j] = element
}
2013-06-23 16:43:44 +02:00
func Monochrome() Color {
const r float64 = 6968.0 / 32768.0
const g float64 = 23434.0 / 32768.0
const b float64 = 2366.0 / 32768.0
return Color{
[colorDim - 1][colorDim]float64{
{r, g, b, 0, 0},
{r, g, b, 0, 0},
{r, g, b, 0, 0},
{0, 0, 0, 1, 0},
},
}
}
2013-12-08 11:38:22 +01:00
func (matrix *Color) Scale(clr color.Color) {
r, g, b, a := clr.RGBA()
rf := float64(r) / float64(math.MaxUint16)
gf := float64(g) / float64(math.MaxUint16)
bf := float64(b) / float64(math.MaxUint16)
af := float64(a) / float64(math.MaxUint16)
for i, e := range []float64{rf, gf, bf, af} {
for j := 0; j < 4; j++ {
matrix.Elements[i][j] *= e
}
}
}