add own NRGBA color format

This commit is contained in:
faiface 2016-12-23 20:25:47 +01:00
parent 33eb8496d8
commit fefa184652
4 changed files with 94 additions and 18 deletions

77
color.go Normal file
View File

@ -0,0 +1,77 @@
package pixel
import "image/color"
// NRGBA represents a non-alpha-premultiplied RGBA color with components within range [0, 1].
//
// The difference between color.NRGBA is that the value range is [0, 1] and the values are floats.
type NRGBA struct {
R, G, B, A float64
}
// Add adds color d to color c component-wise and returns the result (the components are not clamped).
func (c NRGBA) Add(d NRGBA) NRGBA {
return NRGBA{
R: c.R + d.R,
G: c.G + d.G,
B: c.B + d.B,
A: c.A + d.A,
}
}
// Sub subtracts color d from color c component-wise and returns the result (the components are not clamped).
func (c NRGBA) Sub(d NRGBA) NRGBA {
return NRGBA{
R: c.R - d.R,
G: c.G - d.G,
B: c.B - d.B,
A: c.A - d.A,
}
}
// Mul multiplies color c by color d component-wise (the components are not clamped).
func (c NRGBA) Mul(d NRGBA) NRGBA {
return NRGBA{
R: c.R * d.R,
G: c.G * d.G,
B: c.B * d.B,
A: c.A * d.A,
}
}
// Scaled multiplies each component of color c by scale and returns the result (the components are not clamped).
func (c NRGBA) Scaled(scale float64) NRGBA {
return NRGBA{
R: c.R * scale,
G: c.G * scale,
B: c.B * scale,
A: c.A * scale,
}
}
// RGBA returns alpha-premultiplied red, green, blue and alpha components of a color.
func (c NRGBA) RGBA() (r, g, b, a uint32) {
c.R = clamp(c.R, 0, 1)
c.G = clamp(c.G, 0, 1)
c.B = clamp(c.B, 0, 1)
c.A = clamp(c.A, 0, 1)
r = uint32(0xffff * c.R * c.A)
g = uint32(0xffff * c.G * c.A)
b = uint32(0xffff * c.B * c.A)
a = uint32(0xffff * c.A)
return
}
// NRGBAModel converts colors to NRGBA format.
var NRGBAModel = color.ModelFunc(func(c color.Color) color.Color {
r, g, b, a := c.RGBA()
if a == 0 {
return NRGBA{0, 0, 0, 0}
}
return NRGBA{
float64(r) / float64(a),
float64(g) / float64(a),
float64(b) / float64(a),
float64(a) / 0xffff,
}
})

View File

@ -141,8 +141,8 @@ func (s *Shape) Draw(t ...Transform) {
mat = mat.Mul3(s.transform.Mat()) mat = mat.Mul3(s.transform.Mat())
s.parent.Do(func(ctx pixelgl.Context) { s.parent.Do(func(ctx pixelgl.Context) {
r, g, b, a := colorToRGBA(s.color) c := NRGBAModel.Convert(s.color).(NRGBA)
ctx.Shader().SetUniformAttr(maskColorVec4, mgl32.Vec4{r, g, b, a}) ctx.Shader().SetUniformAttr(maskColorVec4, mgl32.Vec4{float32(c.R), float32(c.G), float32(c.B), float32(c.A)})
ctx.Shader().SetUniformAttr(transformMat3, mat) ctx.Shader().SetUniformAttr(transformMat3, mat)
if s.picture != nil { if s.picture != nil {
@ -202,12 +202,12 @@ func NewMultiShape(parent pixelgl.Doer, shapes ...*Shape) *MultiShape {
} }
if color, ok := shapeVertices[vertex][colorVec4]; ok { if color, ok := shapeVertices[vertex][colorVec4]; ok {
color := color.(mgl32.Vec4) color := color.(mgl32.Vec4)
r, g, b, a := colorToRGBA(shape.Color()) c := NRGBAModel.Convert(shape.Color()).(NRGBA)
color = mgl32.Vec4{ color = mgl32.Vec4{
color[0] * r, color[0] * float32(c.R),
color[1] * g, color[1] * float32(c.G),
color[2] * b, color[2] * float32(c.B),
color[3] * a, color[3] * float32(c.A),
} }
shapeVertices[vertex][colorVec4] = color shapeVertices[vertex][colorVec4] = color
} }

18
util.go
View File

@ -1,13 +1,11 @@
package pixel package pixel
import "image/color" func clamp(x, low, high float64) float64 {
if x < low {
// colorToRGBA converts a color from image/color to RGBA components in interval [0, 1]. return low
func colorToRGBA(c color.Color) (r, g, b, a float32) { }
ri, gi, bi, ai := c.RGBA() if x > high {
r = float32(ri) / 0xffff return high
g = float32(gi) / 0xffff }
b = float32(bi) / 0xffff return x
a = float32(ai) / 0xffff
return
} }

View File

@ -150,7 +150,8 @@ func (w *Window) Destroy() {
func (w *Window) Clear(c color.Color) { func (w *Window) Clear(c color.Color) {
w.Do(func(pixelgl.Context) { w.Do(func(pixelgl.Context) {
pixelgl.DoNoBlock(func() { pixelgl.DoNoBlock(func() {
gl.ClearColor(colorToRGBA(c)) c := NRGBAModel.Convert(c).(NRGBA)
gl.ClearColor(float32(c.R), float32(c.G), float32(c.B), float32(c.A))
gl.Clear(gl.COLOR_BUFFER_BIT) gl.Clear(gl.COLOR_BUFFER_BIT)
}) })
}) })