ebiten/colorm.go

148 lines
3.4 KiB
Go
Raw Normal View History

// Copyright 2014 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2014-12-09 15:16:04 +01:00
2014-12-09 14:09:22 +01:00
package ebiten
import (
"math"
)
// ColorMDim is a dimension of a ColorM.
const ColorMDim = 5
2014-12-09 14:09:22 +01:00
var colorMI = ColorM{
initialized: true,
es: [ColorMDim - 1][ColorMDim]float64{
{1, 0, 0, 0, 0},
{0, 1, 0, 0, 0},
{0, 0, 1, 0, 0},
{0, 0, 0, 1, 0},
},
}
// A ColorM represents a matrix to transform coloring when rendering an image.
//
// A ColorM is applied to the source alpha color
// while an Image's pixels' format is alpha premultiplied.
// Before applying a matrix, a color is un-multiplied, and after applying the matrix,
// the color is multiplied again.
2014-12-26 03:31:20 +01:00
//
// The initial value is identity.
type ColorM struct {
initialized bool
es [ColorMDim - 1][ColorMDim]float64
2014-12-26 03:22:36 +01:00
}
func (c *ColorM) dim() int {
return ColorMDim
2014-12-09 14:09:22 +01:00
}
2014-12-14 14:05:44 +01:00
// Element returns a value of a matrix at (i, j).
func (c *ColorM) Element(i, j int) float64 {
if !c.initialized {
2014-12-26 03:22:36 +01:00
if i == j {
return 1
}
return 0
}
2014-12-24 15:26:04 +01:00
return c.es[i][j]
}
2014-12-14 14:05:44 +01:00
// Concat multiplies a color matrix with the other color matrix.
2014-12-29 11:04:18 +01:00
func (c *ColorM) Concat(other ColorM) {
if !c.initialized {
*c = colorMI
2014-12-26 03:22:36 +01:00
}
result := ColorM{}
2014-12-09 14:09:22 +01:00
mul(&other, c, &result)
*c = result
}
2014-12-25 20:22:06 +01:00
// Add adds a color matrix with the other color matrix.
2014-12-29 11:04:18 +01:00
func (c *ColorM) Add(other ColorM) {
if !c.initialized {
*c = colorMI
2014-12-26 03:22:36 +01:00
}
result := ColorM{}
2014-12-25 20:22:06 +01:00
add(&other, c, &result)
*c = result
}
2014-12-26 02:33:50 +01:00
// SetElement sets an element at (i, j).
func (c *ColorM) SetElement(i, j int, element float64) {
if !c.initialized {
*c = colorMI
2014-12-26 03:22:36 +01:00
}
2014-12-24 15:26:04 +01:00
c.es[i][j] = element
2014-12-09 14:09:22 +01:00
}
2014-12-14 14:24:17 +01:00
// Monochrome returns a color matrix to make an image monochrome.
func Monochrome() ColorM {
2014-12-14 17:45:41 +01:00
const r = 6968.0 / 32768.0
const g = 23434.0 / 32768.0
const b = 2366.0 / 32768.0
return ColorM{
initialized: true,
es: [ColorMDim - 1][ColorMDim]float64{
2014-12-09 14:09:22 +01:00
{r, g, b, 0, 0},
{r, g, b, 0, 0},
{r, g, b, 0, 0},
{0, 0, 0, 1, 0},
},
}
}
// ScaleColor returns a color matrix that scales a color matrix by the given color (r, g, b, a).
func ScaleColor(r, g, b, a float64) ColorM {
return ColorM{
initialized: true,
es: [ColorMDim - 1][ColorMDim]float64{
{r, 0, 0, 0, 0},
{0, g, 0, 0, 0},
{0, 0, b, 0, 0},
{0, 0, 0, a, 0},
},
}
}
// TranslateColor returns a color matrix that translates a color matrix by the given color (r, g, b, a).
func TranslateColor(r, g, b, a float64) ColorM {
return ColorM{
initialized: true,
es: [ColorMDim - 1][ColorMDim]float64{
{1, 0, 0, 0, r},
{0, 1, 0, 0, g},
{0, 0, 1, 0, b},
{0, 0, 0, 1, a},
},
}
}
2014-12-14 17:45:41 +01:00
// RotateHue returns a color matrix to rotate the hue
func RotateHue(theta float64) ColorM {
2014-12-14 17:45:41 +01:00
sin, cos := math.Sincos(theta)
2014-12-17 09:10:38 +01:00
v1 := cos + (1.0-cos)/3.0
v2 := (1.0/3.0)*(1.0-cos) - math.Sqrt(1.0/3.0)*sin
v3 := (1.0/3.0)*(1.0-cos) + math.Sqrt(1.0/3.0)*sin
return ColorM{
initialized: true,
es: [ColorMDim - 1][ColorMDim]float64{
2014-12-15 02:55:40 +01:00
{v1, v2, v3, 0, 0},
{v3, v1, v2, 0, 0},
{v2, v3, v1, 0, 0},
2014-12-14 17:45:41 +01:00
{0, 0, 0, 1, 0},
},
}
}