ebiten/internal/affine/colorm.go

238 lines
5.4 KiB
Go
Raw Normal View History

2016-10-31 16:13:19 +01:00
// 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.
package affine
import (
2017-10-14 16:58:09 +02:00
"image/color"
2016-10-31 16:13:19 +01:00
"math"
)
// ColorMDim is a dimension of a ColorM.
const ColorMDim = 5
2017-01-19 16:37:51 +01:00
var (
colorMIdentityElements = []float64{
1, 0, 0, 0, 0,
0, 1, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 1, 0,
2017-01-17 18:23:52 +01:00
}
2017-01-19 16:37:51 +01:00
)
2017-01-17 18:23:52 +01:00
2016-10-31 16:13:19 +01:00
// 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.
//
// The initial value is identity.
type ColorM struct {
2017-01-19 16:37:51 +01:00
// When elements is nil, this matrix is identity.
// elements is immutable and a new array must be created when updating.
elements []float64
2016-10-31 16:13:19 +01:00
}
2017-05-25 15:50:47 +02:00
func (c *ColorM) Reset() {
c.elements = nil
}
2017-10-14 16:58:09 +02:00
func clamp(x float64) float64 {
if x > 1 {
return 1
}
if x < 0 {
return 0
}
return x
}
func (c *ColorM) Apply(clr color.Color) color.Color {
if c.elements == nil {
return clr
}
r, g, b, a := clr.RGBA()
if a == 0 {
return color.Transparent
}
rf := float64(r) / float64(a)
gf := float64(g) / float64(a)
bf := float64(b) / float64(a)
af := float64(a) / 0xffff
e := c.elements
rf2 := e[0]*rf + e[1]*gf + e[2]*bf + e[3]*af + e[4]
gf2 := e[5]*rf + e[6]*gf + e[7]*bf + e[8]*af + e[9]
bf2 := e[10]*rf + e[11]*gf + e[12]*bf + e[13]*af + e[14]
af2 := e[15]*rf + e[16]*gf + e[17]*bf + e[18]*af + e[19]
rf2 = clamp(rf2)
gf2 = clamp(gf2)
bf2 = clamp(bf2)
af2 = clamp(af2)
return color.NRGBA64{
R: uint16(rf2 * 0xffff),
G: uint16(gf2 * 0xffff),
B: uint16(bf2 * 0xffff),
A: uint16(af2 * 0xffff),
}
}
2017-01-19 16:37:51 +01:00
func (c *ColorM) UnsafeElements() []float64 {
if c.elements == nil {
c.elements = colorMIdentityElements
}
return c.elements
2017-01-19 04:07:31 +01:00
}
2017-01-18 18:29:49 +01:00
// SetElement sets an element at (i, j).
func (c *ColorM) SetElement(i, j int, element float64) {
2017-01-19 16:37:51 +01:00
if c.elements == nil {
c.elements = colorMIdentityElements
}
es := make([]float64, len(c.elements))
copy(es, c.elements)
es[i*ColorMDim+j] = element
c.elements = es
2017-01-18 18:29:49 +01:00
}
2016-10-31 16:28:07 +01:00
func (c *ColorM) Equals(other *ColorM) bool {
2017-01-19 16:37:51 +01:00
if c.elements == nil {
if other.elements == nil {
return true
}
c.elements = colorMIdentityElements
}
if other.elements == nil {
other.elements = colorMIdentityElements
2016-10-31 16:28:07 +01:00
}
2017-01-19 16:37:51 +01:00
for i := range c.elements {
if c.elements[i] != other.elements[i] {
return false
}
2016-10-31 16:28:07 +01:00
}
2017-01-19 16:37:51 +01:00
return true
2016-10-31 16:28:07 +01:00
}
2016-10-31 16:13:19 +01:00
// Concat multiplies a color matrix with the other color matrix.
// This is same as muptiplying the matrix other and the matrix c in this order.
2017-05-23 04:30:54 +02:00
func (c *ColorM) Concat(other *ColorM) {
2017-01-19 16:46:25 +01:00
if c.elements == nil {
c.elements = colorMIdentityElements
}
if other.elements == nil {
other.elements = colorMIdentityElements
}
c.elements = mul(other.elements, c.elements, ColorMDim)
2016-10-31 16:13:19 +01:00
}
2017-01-18 18:29:49 +01:00
// Add is deprecated.
2016-10-31 16:13:19 +01:00
func (c *ColorM) Add(other ColorM) {
2017-01-19 16:46:25 +01:00
if c.elements == nil {
c.elements = colorMIdentityElements
}
if other.elements == nil {
other.elements = colorMIdentityElements
}
c.elements = add(other.elements, c.elements, ColorMDim)
2016-10-31 16:13:19 +01:00
}
// Scale scales the matrix by (r, g, b, a).
func (c *ColorM) Scale(r, g, b, a float64) {
2017-01-19 16:37:51 +01:00
if c.elements == nil {
2017-01-19 16:59:33 +01:00
c.elements = []float64{
r, 0, 0, 0, 0,
0, g, 0, 0, 0,
0, 0, b, 0, 0,
0, 0, 0, a, 0,
}
return
2017-01-19 16:37:51 +01:00
}
es := make([]float64, len(c.elements))
copy(es, c.elements)
for i := 0; i < ColorMDim; i++ {
2017-01-19 16:37:51 +01:00
es[i] *= r
es[i+ColorMDim] *= g
es[i+ColorMDim*2] *= b
es[i+ColorMDim*3] *= a
2016-10-31 16:13:19 +01:00
}
2017-01-19 16:37:51 +01:00
c.elements = es
2016-10-31 16:13:19 +01:00
}
// Translate translates the matrix by (r, g, b, a).
func (c *ColorM) Translate(r, g, b, a float64) {
2017-01-19 16:37:51 +01:00
if c.elements == nil {
2017-01-19 16:59:33 +01:00
c.elements = []float64{
1, 0, 0, 0, r,
0, 1, 0, 0, g,
0, 0, 1, 0, b,
0, 0, 0, 1, a,
}
return
2017-01-19 16:37:51 +01:00
}
es := make([]float64, len(c.elements))
copy(es, c.elements)
es[4] += r
es[4+ColorMDim] += g
es[4+ColorMDim*2] += b
es[4+ColorMDim*3] += a
c.elements = es
2016-10-31 16:13:19 +01:00
}
var (
// The YCbCr value ranges are:
// Y: [ 0 - 1 ]
// Cb: [-0.5 - 0.5]
// Cr: [-0.5 - 0.5]
rgbToYCbCr = ColorM{
2017-01-19 16:37:51 +01:00
elements: []float64{
0.2990, 0.5870, 0.1140, 0, 0,
-0.1687, -0.3313, 0.5000, 0, 0,
0.5000, -0.4187, -0.0813, 0, 0,
0, 0, 0, 1, 0,
},
2016-10-31 16:13:19 +01:00
}
yCbCrToRgb = ColorM{
2017-01-19 16:37:51 +01:00
elements: []float64{
1, 0, 1.40200, 0, 0,
1, -0.34414, -0.71414, 0, 0,
1, 1.77200, 0, 0, 0,
0, 0, 0, 1, 0,
},
2016-10-31 16:13:19 +01:00
}
)
2017-01-19 16:37:51 +01:00
// ChangeHSV changes HSV (Hue-Saturation-Value) elements.
2016-10-31 16:13:19 +01:00
// hueTheta is a radian value to ratate hue.
// saturationScale is a value to scale saturation.
// valueScale is a value to scale value (a.k.a. brightness).
//
// This conversion uses RGB to/from YCrCb conversion.
func (c *ColorM) ChangeHSV(hueTheta float64, saturationScale float64, valueScale float64) {
sin, cos := math.Sincos(hueTheta)
2017-05-23 04:30:54 +02:00
c.Concat(&rgbToYCbCr)
c.Concat(&ColorM{
2017-01-19 16:37:51 +01:00
elements: []float64{
1, 0, 0, 0, 0,
0, cos, -sin, 0, 0,
0, sin, cos, 0, 0,
0, 0, 0, 1, 0,
},
2016-10-31 16:13:19 +01:00
})
s := saturationScale
v := valueScale
c.Scale(v, s*v, s*v, 1)
2017-05-23 04:30:54 +02:00
c.Concat(&yCbCrToRgb)
2016-10-31 16:13:19 +01:00
}