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 (
|
2017-10-14 16:58:09 +02:00
|
|
|
"image/color"
|
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/affine"
|
2014-12-09 14:09:22 +01:00
|
|
|
)
|
|
|
|
|
2014-12-26 14:50:02 +01:00
|
|
|
// ColorMDim is a dimension of a ColorM.
|
2016-10-31 16:13:19 +01:00
|
|
|
const ColorMDim = affine.ColorMDim
|
2016-10-30 14:11:50 +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
|
|
|
//
|
2017-09-30 18:59:34 +02:00
|
|
|
// A ColorM is applied to the straight 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 {
|
2021-07-27 03:37:14 +02:00
|
|
|
impl affine.ColorM
|
2019-10-30 17:55:55 +01:00
|
|
|
|
|
|
|
_ [0]func() // Marks as non-comparable.
|
2014-12-09 14:09:22 +01:00
|
|
|
}
|
|
|
|
|
2021-07-27 03:37:14 +02:00
|
|
|
func (c *ColorM) affineColorM() affine.ColorM {
|
|
|
|
if c.impl != nil {
|
|
|
|
return c.impl
|
|
|
|
}
|
|
|
|
return affine.ColorMIdentity{}
|
|
|
|
}
|
|
|
|
|
2018-05-07 18:53:59 +02:00
|
|
|
// String returns a string representation of ColorM.
|
|
|
|
func (c *ColorM) String() string {
|
2022-06-30 04:13:02 +02:00
|
|
|
return c.affineColorM().String()
|
2018-05-07 18:53:59 +02:00
|
|
|
}
|
|
|
|
|
2017-05-25 15:50:47 +02:00
|
|
|
// Reset resets the ColorM as identity.
|
|
|
|
func (c *ColorM) Reset() {
|
2021-07-27 03:37:14 +02:00
|
|
|
c.impl = affine.ColorMIdentity{}
|
2017-05-25 15:50:47 +02:00
|
|
|
}
|
|
|
|
|
2017-10-14 16:58:09 +02:00
|
|
|
// Apply pre-multiplies a vector (r, g, b, a, 1) by the matrix
|
2017-10-14 17:00:44 +02:00
|
|
|
// where r, g, b, and a are clr's values in straight-alpha format.
|
2017-10-14 16:58:09 +02:00
|
|
|
// In other words, Apply calculates ColorM * (r, g, b, a, 1)^T.
|
|
|
|
func (c *ColorM) Apply(clr color.Color) color.Color {
|
2021-07-27 03:37:14 +02:00
|
|
|
return c.affineColorM().Apply(clr)
|
2017-10-14 16:58:09 +02: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) {
|
2021-08-17 18:09:04 +02:00
|
|
|
o := other.impl
|
|
|
|
if o == nil {
|
2021-08-17 18:39:04 +02:00
|
|
|
return
|
2021-08-17 18:09:04 +02:00
|
|
|
}
|
|
|
|
c.impl = c.affineColorM().Concat(o)
|
2014-12-09 14:09:22 +01:00
|
|
|
}
|
|
|
|
|
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) {
|
2021-07-27 03:37:14 +02:00
|
|
|
c.impl = c.affineColorM().Scale(float32(r), float32(g), float32(b), float32(a))
|
2015-01-03 10:35:44 +01:00
|
|
|
}
|
|
|
|
|
2022-01-10 09:36:42 +01:00
|
|
|
// ScaleWithColor scales the matrix by clr.
|
|
|
|
func (c *ColorM) ScaleWithColor(clr color.Color) {
|
|
|
|
cr, cg, cb, ca := clr.RGBA()
|
|
|
|
if ca == 0 {
|
|
|
|
c.Scale(0, 0, 0, 0)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.Scale(float64(cr)/float64(ca), float64(cg)/float64(ca), float64(cb)/float64(ca), float64(ca)/0xffff)
|
|
|
|
}
|
|
|
|
|
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) {
|
2021-07-27 03:37:14 +02:00
|
|
|
c.impl = c.affineColorM().Translate(float32(r), float32(g), float32(b), float32(a))
|
2015-01-03 10:35:44 +01:00
|
|
|
}
|
|
|
|
|
2016-02-06 15:56:02 +01:00
|
|
|
// RotateHue rotates the hue.
|
2017-09-30 18:59:34 +02:00
|
|
|
// theta represents rotating angle in radian.
|
2016-02-06 15:56:02 +01:00
|
|
|
func (c *ColorM) RotateHue(theta float64) {
|
2017-09-30 18:59:34 +02:00
|
|
|
c.ChangeHSV(theta, 1, 1)
|
2016-03-03 17:35:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ChangeHSV changes HSV (Hue-Saturation-Value) values.
|
2018-10-12 17:07:48 +02:00
|
|
|
// hueTheta is a radian value to rotate hue.
|
2016-03-03 17:35:03 +01:00
|
|
|
// 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) {
|
2021-07-27 03:37:14 +02:00
|
|
|
c.impl = affine.ChangeHSV(c.affineColorM(), hueTheta, float32(saturationScale), float32(valueScale))
|
2016-02-06 15:56:02 +01:00
|
|
|
}
|
|
|
|
|
2016-10-31 16:13:19 +01:00
|
|
|
// Element returns a value of a matrix at (i, j).
|
|
|
|
func (c *ColorM) Element(i, j int) float64 {
|
2021-09-04 10:55:47 +02:00
|
|
|
return float64(c.affineColorM().At(i, j))
|
2016-10-31 16:13:19 +01:00
|
|
|
}
|
2016-03-03 17:35:03 +01:00
|
|
|
|
2016-10-31 16:13:19 +01:00
|
|
|
// SetElement sets an element at (i, j).
|
2017-09-30 18:59:34 +02:00
|
|
|
func (c *ColorM) SetElement(i, j int, element float64) {
|
2021-07-27 03:37:14 +02:00
|
|
|
c.impl = affine.ColorMSetElement(c.affineColorM(), i, j, float32(element))
|
2016-03-03 17:35:03 +01:00
|
|
|
}
|
|
|
|
|
2020-07-03 18:23:47 +02:00
|
|
|
// IsInvertible returns a boolean value indicating
|
|
|
|
// whether the matrix c is invertible or not.
|
|
|
|
func (c *ColorM) IsInvertible() bool {
|
2021-07-27 03:37:14 +02:00
|
|
|
return c.affineColorM().IsInvertible()
|
2020-07-03 18:23:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Invert inverts the matrix.
|
|
|
|
// If c is not invertible, Invert panics.
|
|
|
|
func (c *ColorM) Invert() {
|
2021-07-27 03:37:14 +02:00
|
|
|
c.impl = c.affineColorM().Invert()
|
2020-07-03 18:23:47 +02:00
|
|
|
}
|