mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-11 19:48:54 +01:00
affine: Revert ColorM implementation to use float64 array
This commit is contained in:
parent
4915531450
commit
edf336499c
@ -16,48 +16,11 @@ package affine
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"math"
|
"math"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/internal/endian"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ColorMDim is a dimension of a ColorM.
|
// ColorMDim is a dimension of a ColorM.
|
||||||
const ColorMDim = 5
|
const ColorMDim = 5
|
||||||
|
|
||||||
func uint64ToBytes(value uint64) []uint8 {
|
|
||||||
result := make([]uint8, 8)
|
|
||||||
if endian.IsLittle() {
|
|
||||||
for i := 0; i < 8; i++ {
|
|
||||||
result[i] = uint8(value)
|
|
||||||
value >>= 8
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for i := 7; 0 <= i; i-- {
|
|
||||||
result[i] = uint8(value)
|
|
||||||
value >>= 8
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
func colorMValueString(values [ColorMDim - 1][ColorMDim]float64) string {
|
|
||||||
b := make([]uint8, 0, (ColorMDim-1)*(ColorMDim)*8)
|
|
||||||
for i := 0; i < ColorMDim-1; i++ {
|
|
||||||
for j := 0; j < ColorMDim; j++ {
|
|
||||||
b = append(b, uint64ToBytes(math.Float64bits(values[i][j]))...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return string(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
colorMIdentityValue = colorMValueString([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 represents a matrix to transform coloring when rendering an image.
|
||||||
//
|
//
|
||||||
// A ColorM is applied to the source alpha color
|
// A ColorM is applied to the source alpha color
|
||||||
@ -67,22 +30,48 @@ var (
|
|||||||
//
|
//
|
||||||
// The initial value is identity.
|
// The initial value is identity.
|
||||||
type ColorM struct {
|
type ColorM struct {
|
||||||
// when values is empty, this matrix is identity.
|
initialized bool
|
||||||
values string
|
es [(ColorMDim - 1) * (ColorMDim)]float64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ColorM) dim() int {
|
func (c *ColorM) dim() int {
|
||||||
return ColorMDim
|
return ColorMDim
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *ColorM) initialize() {
|
||||||
|
c.initialized = true
|
||||||
|
for i := 0; i < ColorMDim-1; i++ {
|
||||||
|
c.es[i*ColorMDim+i] = 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Element returns a value of a matrix at (i, j).
|
||||||
|
func (c *ColorM) Element(i, j int) float64 {
|
||||||
|
if !c.initialized {
|
||||||
|
if i == j {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return c.es[i*ColorMDim+j]
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetElement sets an element at (i, j).
|
||||||
|
func (c *ColorM) SetElement(i, j int, element float64) {
|
||||||
|
if !c.initialized {
|
||||||
|
c.initialize()
|
||||||
|
}
|
||||||
|
c.es[i*ColorMDim+j] = element
|
||||||
|
}
|
||||||
|
|
||||||
func (c *ColorM) Equals(other *ColorM) bool {
|
func (c *ColorM) Equals(other *ColorM) bool {
|
||||||
if c.values == "" {
|
if !c.initialized {
|
||||||
c.values = colorMIdentityValue
|
c.initialize()
|
||||||
}
|
}
|
||||||
if other.values == "" {
|
if !other.initialized {
|
||||||
other.values = colorMIdentityValue
|
other.initialize()
|
||||||
}
|
}
|
||||||
return c.values == other.values
|
return c.es == other.es
|
||||||
}
|
}
|
||||||
|
|
||||||
// Concat multiplies a color matrix with the other color matrix.
|
// Concat multiplies a color matrix with the other color matrix.
|
||||||
@ -130,20 +119,22 @@ var (
|
|||||||
// Cr: [-0.5 - 0.5]
|
// Cr: [-0.5 - 0.5]
|
||||||
|
|
||||||
rgbToYCbCr = ColorM{
|
rgbToYCbCr = ColorM{
|
||||||
values: colorMValueString([ColorMDim - 1][ColorMDim]float64{
|
initialized: true,
|
||||||
{0.2990, 0.5870, 0.1140, 0, 0},
|
es: [...]float64{
|
||||||
{-0.1687, -0.3313, 0.5000, 0, 0},
|
0.2990, 0.5870, 0.1140, 0, 0,
|
||||||
{0.5000, -0.4187, -0.0813, 0, 0},
|
-0.1687, -0.3313, 0.5000, 0, 0,
|
||||||
{0, 0, 0, 1, 0},
|
0.5000, -0.4187, -0.0813, 0, 0,
|
||||||
}),
|
0, 0, 0, 1, 0,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
yCbCrToRgb = ColorM{
|
yCbCrToRgb = ColorM{
|
||||||
values: colorMValueString([ColorMDim - 1][ColorMDim]float64{
|
initialized: true,
|
||||||
{1, 0, 1.40200, 0, 0},
|
es: [...]float64{
|
||||||
{1, -0.34414, -0.71414, 0, 0},
|
1, 0, 1.40200, 0, 0,
|
||||||
{1, 1.77200, 0, 0, 0},
|
1, -0.34414, -0.71414, 0, 0,
|
||||||
{0, 0, 0, 1, 0},
|
1, 1.77200, 0, 0, 0,
|
||||||
}),
|
0, 0, 0, 1, 0,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -157,12 +148,13 @@ func (c *ColorM) ChangeHSV(hueTheta float64, saturationScale float64, valueScale
|
|||||||
sin, cos := math.Sincos(hueTheta)
|
sin, cos := math.Sincos(hueTheta)
|
||||||
c.Concat(rgbToYCbCr)
|
c.Concat(rgbToYCbCr)
|
||||||
c.Concat(ColorM{
|
c.Concat(ColorM{
|
||||||
values: colorMValueString([ColorMDim - 1][ColorMDim]float64{
|
initialized: true,
|
||||||
{1, 0, 0, 0, 0},
|
es: [...]float64{
|
||||||
{0, cos, -sin, 0, 0},
|
1, 0, 0, 0, 0,
|
||||||
{0, sin, cos, 0, 0},
|
0, cos, -sin, 0, 0,
|
||||||
{0, 0, 0, 1, 0},
|
0, sin, cos, 0, 0,
|
||||||
}),
|
0, 0, 0, 1, 0,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
s := saturationScale
|
s := saturationScale
|
||||||
v := valueScale
|
v := valueScale
|
||||||
@ -184,24 +176,26 @@ func Monochrome() ColorM {
|
|||||||
// ScaleColor is deprecated as of 1.2.0-alpha. Use Scale instead.
|
// ScaleColor is deprecated as of 1.2.0-alpha. Use Scale instead.
|
||||||
func ScaleColor(r, g, b, a float64) ColorM {
|
func ScaleColor(r, g, b, a float64) ColorM {
|
||||||
return ColorM{
|
return ColorM{
|
||||||
values: colorMValueString([ColorMDim - 1][ColorMDim]float64{
|
initialized: true,
|
||||||
{r, 0, 0, 0, 0},
|
es: [...]float64{
|
||||||
{0, g, 0, 0, 0},
|
r, 0, 0, 0, 0,
|
||||||
{0, 0, b, 0, 0},
|
0, g, 0, 0, 0,
|
||||||
{0, 0, 0, a, 0},
|
0, 0, b, 0, 0,
|
||||||
}),
|
0, 0, 0, a, 0,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TranslateColor is deprecated as of 1.2.0-alpha. Use Translate instead.
|
// TranslateColor is deprecated as of 1.2.0-alpha. Use Translate instead.
|
||||||
func TranslateColor(r, g, b, a float64) ColorM {
|
func TranslateColor(r, g, b, a float64) ColorM {
|
||||||
return ColorM{
|
return ColorM{
|
||||||
values: colorMValueString([ColorMDim - 1][ColorMDim]float64{
|
initialized: true,
|
||||||
{1, 0, 0, 0, r},
|
es: [...]float64{
|
||||||
{0, 1, 0, 0, g},
|
1, 0, 0, 0, r,
|
||||||
{0, 0, 1, 0, b},
|
0, 1, 0, 0, g,
|
||||||
{0, 0, 0, 1, a},
|
0, 0, 1, 0, b,
|
||||||
}),
|
0, 0, 0, 1, a,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,46 +0,0 @@
|
|||||||
// Copyright 2016 The Ebiten Authors
|
|
||||||
//
|
|
||||||
// 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.
|
|
||||||
|
|
||||||
// +build js
|
|
||||||
|
|
||||||
package affine
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/gopherjs/gopherjs/js"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Element returns a value of a matrix at (i, j).
|
|
||||||
func (c *ColorM) Element(i, j int) float64 {
|
|
||||||
if c.values == "" {
|
|
||||||
if i == j {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
a := js.NewArrayBuffer([]uint8(c.values))
|
|
||||||
af64 := js.Global.Get("Float64Array").New(a)
|
|
||||||
return af64.Index(i*ColorMDim + j).Float()
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetElement sets an element at (i, j).
|
|
||||||
func (c *ColorM) SetElement(i, j int, value float64) {
|
|
||||||
if c.values == "" {
|
|
||||||
c.values = colorMIdentityValue
|
|
||||||
}
|
|
||||||
a := js.NewArrayBuffer([]uint8(c.values))
|
|
||||||
a8 := js.Global.Get("Uint8Array").New(a)
|
|
||||||
af64 := js.Global.Get("Float64Array").New(a)
|
|
||||||
af64.SetIndex(i*ColorMDim+j, value)
|
|
||||||
c.values = string(a8.Interface().([]uint8))
|
|
||||||
}
|
|
@ -1,57 +0,0 @@
|
|||||||
// Copyright 2016 The Ebiten Authors
|
|
||||||
//
|
|
||||||
// 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.
|
|
||||||
|
|
||||||
// +build !js
|
|
||||||
|
|
||||||
package affine
|
|
||||||
|
|
||||||
import (
|
|
||||||
"math"
|
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/internal/endian"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Element returns a value of a matrix at (i, j).
|
|
||||||
func (c *ColorM) Element(i, j int) float64 {
|
|
||||||
if c.values == "" {
|
|
||||||
if i == j {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
offset := 8 * (i*ColorMDim + j)
|
|
||||||
v := uint64(0)
|
|
||||||
if endian.IsLittle() {
|
|
||||||
for k := 7; 0 <= k; k-- {
|
|
||||||
v <<= 8
|
|
||||||
v += uint64(c.values[offset+k])
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for k := 0; k < 8; k++ {
|
|
||||||
v <<= 8
|
|
||||||
v += uint64(c.values[offset+k])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return math.Float64frombits(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetElement sets an element at (i, j).
|
|
||||||
func (c *ColorM) SetElement(i, j int, value float64) {
|
|
||||||
if c.values == "" {
|
|
||||||
c.values = colorMIdentityValue
|
|
||||||
}
|
|
||||||
b := uint64ToBytes(math.Float64bits(value))
|
|
||||||
offset := 8 * (i*ColorMDim + j)
|
|
||||||
c.values = c.values[:offset] + string(b) + c.values[offset+8:]
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user