2014-12-24 03:04:10 +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.
|
2014-12-09 15:16:04 +01:00
|
|
|
|
2014-12-09 14:09:22 +01:00
|
|
|
package ebiten
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math"
|
|
|
|
)
|
|
|
|
|
2014-12-26 14:50:02 +01:00
|
|
|
// ColorMDim is a dimension of a ColorM.
|
|
|
|
const ColorMDim = 5
|
2014-12-09 14:09:22 +01:00
|
|
|
|
2014-12-26 14:50:02 +01:00
|
|
|
// A ColorM represents a matrix to transform coloring when rendering an image.
|
2014-12-22 15:08:13 +01:00
|
|
|
//
|
2014-12-26 14:50:02 +01:00
|
|
|
// A ColorM is applied to the source alpha color
|
2014-12-22 15:08:13 +01:00
|
|
|
// 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.
|
2014-12-26 14:50:02 +01:00
|
|
|
type ColorM struct {
|
2014-12-26 03:43:31 +01:00
|
|
|
initialized bool
|
2014-12-26 14:50:02 +01:00
|
|
|
es [ColorMDim - 1][ColorMDim]float64
|
2014-12-26 03:22:36 +01:00
|
|
|
}
|
|
|
|
|
2015-01-02 17:54:12 +01:00
|
|
|
func (c *ColorM) dim() int {
|
2014-12-26 14:50:02 +01:00
|
|
|
return ColorMDim
|
2014-12-09 14:09:22 +01:00
|
|
|
}
|
|
|
|
|
2015-01-03 14:54:01 +01:00
|
|
|
func (c *ColorM) initialize() {
|
|
|
|
c.initialized = true
|
|
|
|
c.es[0][0] = 1
|
|
|
|
c.es[1][1] = 1
|
|
|
|
c.es[2][2] = 1
|
|
|
|
c.es[3][3] = 1
|
|
|
|
}
|
|
|
|
|
2014-12-14 14:05:44 +01:00
|
|
|
// Element returns a value of a matrix at (i, j).
|
2015-01-02 17:54:12 +01:00
|
|
|
func (c *ColorM) Element(i, j int) float64 {
|
2014-12-26 03:43:31 +01:00
|
|
|
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-13 06:41:38 +01:00
|
|
|
}
|
|
|
|
|
2014-12-14 14:05:44 +01:00
|
|
|
// Concat multiplies a color matrix with the other color matrix.
|
2016-02-15 15:08:10 +01:00
|
|
|
// This is same as muptiplying the matrix other and the matrix c in this order.
|
2014-12-29 11:04:18 +01:00
|
|
|
func (c *ColorM) Concat(other ColorM) {
|
2014-12-26 03:43:31 +01:00
|
|
|
if !c.initialized {
|
2015-01-03 14:54:01 +01:00
|
|
|
c.initialize()
|
2014-12-26 03:22:36 +01:00
|
|
|
}
|
2014-12-26 14:50:02 +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) {
|
2014-12-26 03:43:31 +01:00
|
|
|
if !c.initialized {
|
2015-01-03 14:54:01 +01:00
|
|
|
c.initialize()
|
2014-12-26 03:22:36 +01:00
|
|
|
}
|
2014-12-26 14:50:02 +01:00
|
|
|
result := ColorM{}
|
2014-12-25 20:22:06 +01:00
|
|
|
add(&other, c, &result)
|
|
|
|
*c = result
|
|
|
|
}
|
|
|
|
|
2016-02-15 20:46:28 +01:00
|
|
|
// Scale scales the matrix by (r, g, b, a).
|
2015-01-03 10:35:44 +01:00
|
|
|
func (c *ColorM) Scale(r, g, b, a float64) {
|
|
|
|
if !c.initialized {
|
2015-01-03 14:54:01 +01:00
|
|
|
c.initialize()
|
2015-01-03 10:35:44 +01:00
|
|
|
}
|
2015-01-03 11:18:09 +01:00
|
|
|
for i := 0; i < ColorMDim; i++ {
|
|
|
|
c.es[0][i] *= r
|
|
|
|
c.es[1][i] *= g
|
|
|
|
c.es[2][i] *= b
|
|
|
|
c.es[3][i] *= a
|
|
|
|
}
|
2015-01-03 10:35:44 +01:00
|
|
|
}
|
|
|
|
|
2016-02-15 20:46:28 +01:00
|
|
|
// Translate translates the matrix by (r, g, b, a).
|
2015-01-03 10:35:44 +01:00
|
|
|
func (c *ColorM) Translate(r, g, b, a float64) {
|
|
|
|
if !c.initialized {
|
2015-01-03 14:54:01 +01:00
|
|
|
c.initialize()
|
2015-01-03 10:35:44 +01:00
|
|
|
}
|
|
|
|
c.es[0][4] += r
|
|
|
|
c.es[1][4] += g
|
|
|
|
c.es[2][4] += b
|
|
|
|
c.es[3][4] += a
|
|
|
|
}
|
|
|
|
|
2016-02-06 15:56:02 +01:00
|
|
|
// RotateHue rotates the hue.
|
|
|
|
func (c *ColorM) RotateHue(theta float64) {
|
2016-03-03 17:35:03 +01:00
|
|
|
c.ChangeHSV(theta, 1, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
// The YCbCr value ranges are:
|
|
|
|
// Y: [ 0 - 1 ]
|
|
|
|
// Cb: [-0.5 - 0.5]
|
|
|
|
// Cr: [-0.5 - 0.5]
|
|
|
|
|
|
|
|
rgbToYCbCr = ColorM{
|
|
|
|
initialized: true,
|
|
|
|
es: [ColorMDim - 1][ColorMDim]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},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
yCbCrToRgb = ColorM{
|
|
|
|
initialized: true,
|
|
|
|
es: [ColorMDim - 1][ColorMDim]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},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// ChangeHSV changes HSV (Hue-Saturation-Value) values.
|
|
|
|
// 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)
|
|
|
|
c.Concat(rgbToYCbCr)
|
2016-02-06 15:56:02 +01:00
|
|
|
c.Concat(ColorM{
|
|
|
|
initialized: true,
|
|
|
|
es: [ColorMDim - 1][ColorMDim]float64{
|
2016-03-03 17:35:03 +01:00
|
|
|
{1, 0, 0, 0, 0},
|
|
|
|
{0, cos, -sin, 0, 0},
|
|
|
|
{0, sin, cos, 0, 0},
|
2016-02-06 15:56:02 +01:00
|
|
|
{0, 0, 0, 1, 0},
|
|
|
|
},
|
|
|
|
})
|
2016-03-03 17:35:03 +01:00
|
|
|
s := saturationScale
|
|
|
|
v := valueScale
|
|
|
|
c.Scale(v, s*v, s*v, 1)
|
|
|
|
c.Concat(yCbCrToRgb)
|
2016-02-06 15:56:02 +01:00
|
|
|
}
|
|
|
|
|
2014-12-26 02:33:50 +01:00
|
|
|
// SetElement sets an element at (i, j).
|
2014-12-26 14:50:02 +01:00
|
|
|
func (c *ColorM) SetElement(i, j int, element float64) {
|
2014-12-26 03:43:31 +01:00
|
|
|
if !c.initialized {
|
2015-01-03 14:54:01 +01:00
|
|
|
c.initialize()
|
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
|
|
|
}
|
|
|
|
|
2016-03-03 17:35:03 +01:00
|
|
|
var monochrome ColorM
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
monochrome.ChangeHSV(0, 0, 1)
|
|
|
|
}
|
|
|
|
|
2014-12-14 14:24:17 +01:00
|
|
|
// Monochrome returns a color matrix to make an image monochrome.
|
2014-12-26 14:50:02 +01:00
|
|
|
func Monochrome() ColorM {
|
2016-03-03 17:35:03 +01:00
|
|
|
return monochrome
|
2014-12-09 14:09:22 +01:00
|
|
|
}
|
|
|
|
|
2016-02-06 13:49:11 +01:00
|
|
|
// Deprecated as of 1.2.0-alpha. Use Scale instead.
|
2014-12-26 14:50:02 +01:00
|
|
|
func ScaleColor(r, g, b, a float64) ColorM {
|
|
|
|
return ColorM{
|
2014-12-26 03:43:31 +01:00
|
|
|
initialized: true,
|
2014-12-26 14:50:02 +01:00
|
|
|
es: [ColorMDim - 1][ColorMDim]float64{
|
2014-12-21 16:02:14 +01:00
|
|
|
{r, 0, 0, 0, 0},
|
|
|
|
{0, g, 0, 0, 0},
|
|
|
|
{0, 0, b, 0, 0},
|
|
|
|
{0, 0, 0, a, 0},
|
2014-12-14 18:04:33 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-06 13:49:11 +01:00
|
|
|
// Deprecated as of 1.2.0-alpha. Use Translate instead.
|
2014-12-26 14:50:02 +01:00
|
|
|
func TranslateColor(r, g, b, a float64) ColorM {
|
|
|
|
return ColorM{
|
2014-12-26 03:43:31 +01:00
|
|
|
initialized: true,
|
2014-12-26 14:50:02 +01:00
|
|
|
es: [ColorMDim - 1][ColorMDim]float64{
|
2014-12-21 16:02:14 +01:00
|
|
|
{1, 0, 0, 0, r},
|
|
|
|
{0, 1, 0, 0, g},
|
|
|
|
{0, 0, 1, 0, b},
|
|
|
|
{0, 0, 0, 1, a},
|
2014-12-14 18:04:33 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-06 15:56:02 +01:00
|
|
|
// Deprecated as of 1.2.0-alpha. Use RotateHue member function instead.
|
2014-12-26 14:50:02 +01:00
|
|
|
func RotateHue(theta float64) ColorM {
|
2016-02-06 18:39:52 +01:00
|
|
|
c := ColorM{}
|
|
|
|
c.RotateHue(theta)
|
|
|
|
return c
|
2014-12-14 17:45:41 +01:00
|
|
|
}
|