ebiten/graphics/matrix/color.go

86 lines
1.6 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"
)
const ColorDim = 5
2013-06-20 18:47:39 +02:00
type Color struct {
Elements [ColorDim - 1][ColorDim]float64
2013-06-20 18:47:39 +02:00
}
2014-12-06 15:03:17 +01:00
func ColorI() Color {
2013-06-20 18:47:39 +02:00
return Color{
[ColorDim - 1][ColorDim]float64{
2013-06-20 18:47:39 +02:00
{1, 0, 0, 0, 0},
{0, 1, 0, 0, 0},
{0, 0, 1, 0, 0},
{0, 0, 0, 1, 0},
},
}
}
2014-12-07 15:20:41 +01:00
func (matrix *Color) dim() int {
return ColorDim
2013-06-20 18:47:39 +02:00
}
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{
2013-06-23 16:43:44 +02:00
{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
2013-12-18 19:21:25 +01:00
func rgba(clr color.Color) (float64, float64, float64, float64) {
2013-12-08 11:38:22 +01:00
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)
2013-12-18 19:21:25 +01:00
return rf, gf, bf, af
}
func (matrix *Color) Scale(clr color.Color) {
rf, gf, bf, af := rgba(clr)
2013-12-08 11:38:22 +01:00
for i, e := range []float64{rf, gf, bf, af} {
for j := 0; j < 4; j++ {
matrix.Elements[i][j] *= e
}
}
}
2013-12-18 19:21:25 +01:00
func (matrix *Color) Translate(clr color.Color) {
rf, gf, bf, af := rgba(clr)
matrix.Elements[0][4] = rf
matrix.Elements[1][4] = gf
matrix.Elements[2][4] = bf
matrix.Elements[3][4] = af
}