changed gl color from four floats to a struct type

This commit is contained in:
Thomas Friedel 2018-02-07 11:37:08 +01:00
parent d3dc4cc3b7
commit 9ed7a36cbc
4 changed files with 67 additions and 66 deletions

View file

@ -36,11 +36,11 @@ type pathPoint struct {
type drawState struct { type drawState struct {
transform lm.Mat3x3 transform lm.Mat3x3
fill struct { fill struct {
r, g, b, a float32 color glColor
} }
stroke struct { stroke struct {
r, g, b, a float32 color glColor
lineWidth float32 lineWidth float32
} }
font *Font font *Font
fontSize float32 fontSize float32
@ -221,19 +221,17 @@ func glError() error {
// SetFillColor sets the color for any fill calls // SetFillColor sets the color for any fill calls
func (cv *Canvas) SetFillColor(value ...interface{}) { func (cv *Canvas) SetFillColor(value ...interface{}) {
r, g, b, a, ok := parseColor(value...) c, ok := parseColor(value...)
if ok { if ok {
f := &cv.state.fill cv.state.fill.color = c
f.r, f.g, f.b, f.a = r, g, b, a
} }
} }
// SetStrokeColor sets the color for any line drawing calls // SetStrokeColor sets the color for any line drawing calls
func (cv *Canvas) SetStrokeColor(value ...interface{}) { func (cv *Canvas) SetStrokeColor(value ...interface{}) {
r, g, b, a, ok := parseColor(value...) c, ok := parseColor(value...)
if ok { if ok {
s := &cv.state.stroke cv.state.stroke.color = c
s.r, s.g, s.b, s.a = r, g, b, a
} }
} }
@ -334,8 +332,8 @@ func (cv *Canvas) FillRect(x, y, w, h float32) {
gli.BufferData(gl_ARRAY_BUFFER, len(data)*4, unsafe.Pointer(&data[0]), gl_STREAM_DRAW) gli.BufferData(gl_ARRAY_BUFFER, len(data)*4, unsafe.Pointer(&data[0]), gl_STREAM_DRAW)
gli.VertexAttribPointer(sr.vertex, 2, gl_FLOAT, false, 0, nil) gli.VertexAttribPointer(sr.vertex, 2, gl_FLOAT, false, 0, nil)
f := cv.state.fill c := cv.state.fill.color
gli.Uniform4f(sr.color, f.r, f.g, f.b, f.a) gli.Uniform4f(sr.color, c.r, c.g, c.b, c.a)
gli.EnableVertexAttribArray(sr.vertex) gli.EnableVertexAttribArray(sr.vertex)
gli.DrawArrays(gl_TRIANGLE_FAN, 0, 4) gli.DrawArrays(gl_TRIANGLE_FAN, 0, 4)
gli.DisableVertexAttribArray(sr.vertex) gli.DisableVertexAttribArray(sr.vertex)

View file

@ -7,41 +7,46 @@ import (
"strings" "strings"
) )
func colorGoToGL(color color.Color) (r, g, b, a float32) { type glColor struct {
ir, ig, ib, ia := color.RGBA() r, g, b, a float32
r = float32(ir) / 65535
g = float32(ig) / 65535
b = float32(ib) / 65535
a = float32(ia) / 65535
return
} }
func colorGLToGo(r, g, b, a float32) color.Color { func colorGoToGL(color color.Color) glColor {
if r < 0 { ir, ig, ib, ia := color.RGBA()
r = 0 var c glColor
} else if r > 1 { c.r = float32(ir) / 65535
r = 1 c.g = float32(ig) / 65535
c.b = float32(ib) / 65535
c.a = float32(ia) / 65535
return c
}
func colorGLToGo(c glColor) color.Color {
if c.r < 0 {
c.r = 0
} else if c.r > 1 {
c.r = 1
} }
if g < 0 { if c.g < 0 {
g = 0 c.g = 0
} else if g > 1 { } else if c.g > 1 {
g = 1 c.g = 1
} }
if b < 0 { if c.b < 0 {
b = 0 c.b = 0
} else if b > 1 { } else if c.b > 1 {
b = 1 c.b = 1
} }
if a < 0 { if c.a < 0 {
a = 0 c.a = 0
} else if a > 1 { } else if c.a > 1 {
a = 1 c.a = 1
} }
return color.RGBA{ return color.RGBA{
R: uint8(r * 255), R: uint8(c.r * 255),
G: uint8(g * 255), G: uint8(c.g * 255),
B: uint8(b * 255), B: uint8(c.b * 255),
A: uint8(a * 255), A: uint8(c.a * 255),
} }
} }
@ -117,34 +122,33 @@ func parseColorComponent(value interface{}) (float32, bool) {
return 0, false return 0, false
} }
func parseColor(value ...interface{}) (r, g, b, a float32, ok bool) { func parseColor(value ...interface{}) (c glColor, ok bool) {
a = 1
if len(value) == 1 { if len(value) == 1 {
switch v := value[0].(type) { switch v := value[0].(type) {
case color.Color: case color.Color:
r, g, b, a = colorGoToGL(v) c = colorGoToGL(v)
ok = true ok = true
return return
case [3]float32: case [3]float32:
return v[0], v[1], v[2], 1, true return glColor{r: v[0], g: v[1], b: v[2], a: 1}, true
case [4]float32: case [4]float32:
return v[0], v[1], v[2], v[3], true return glColor{r: v[0], g: v[1], b: v[2], a: v[3]}, true
case [3]float64: case [3]float64:
return float32(v[0]), float32(v[1]), float32(v[2]), 1, true return glColor{r: float32(v[0]), g: float32(v[1]), b: float32(v[2]), a: 1}, true
case [4]float64: case [4]float64:
return float32(v[0]), float32(v[1]), float32(v[2]), float32(v[3]), true return glColor{r: float32(v[0]), g: float32(v[1]), b: float32(v[2]), a: float32(v[3])}, true
case [3]int: case [3]int:
return float32(v[0]) / 255, float32(v[1]) / 255, float32(v[2]) / 255, 1, true return glColor{r: float32(v[0]) / 255, g: float32(v[1]) / 255, b: float32(v[2]) / 255, a: 1}, true
case [4]int: case [4]int:
return float32(v[0]) / 255, float32(v[1]) / 255, float32(v[2]) / 255, float32(v[3]) / 255, true return glColor{r: float32(v[0]) / 255, g: float32(v[1]) / 255, b: float32(v[2]) / 255, a: float32(v[3]) / 255}, true
case [3]uint: case [3]uint:
return float32(v[0]) / 255, float32(v[1]) / 255, float32(v[2]) / 255, 1, true return glColor{r: float32(v[0]) / 255, g: float32(v[1]) / 255, b: float32(v[2]) / 255, a: 1}, true
case [4]uint: case [4]uint:
return float32(v[0]) / 255, float32(v[1]) / 255, float32(v[2]) / 255, float32(v[3]) / 255, true return glColor{r: float32(v[0]) / 255, g: float32(v[1]) / 255, b: float32(v[2]) / 255, a: float32(v[3]) / 255}, true
case [3]uint8: case [3]uint8:
return float32(v[0]) / 255, float32(v[1]) / 255, float32(v[2]) / 255, 1, true return glColor{r: float32(v[0]) / 255, g: float32(v[1]) / 255, b: float32(v[2]) / 255, a: 1}, true
case [4]uint8: case [4]uint8:
return float32(v[0]) / 255, float32(v[1]) / 255, float32(v[2]) / 255, float32(v[3]) / 255, true return glColor{r: float32(v[0]) / 255, g: float32(v[1]) / 255, b: float32(v[2]) / 255, a: float32(v[3]) / 255}, true
case string: case string:
if len(v) == 0 { if len(v) == 0 {
return return
@ -176,7 +180,7 @@ func parseColor(value ...interface{}) (r, g, b, a float32, ok bool) {
} }
ia = ia*16 + ia ia = ia*16 + ia
} }
return float32(ir) / 255, float32(ig) / 255, float32(ib) / 255, float32(ia) / 255, true return glColor{r: float32(ir) / 255, g: float32(ig) / 255, b: float32(ib) / 255, a: float32(ia) / 255}, true
} else if len(str) == 6 || len(str) == 8 { } else if len(str) == 6 || len(str) == 8 {
var ir, ig, ib int var ir, ig, ib int
ia := 255 ia := 255
@ -198,7 +202,7 @@ func parseColor(value ...interface{}) (r, g, b, a float32, ok bool) {
return return
} }
} }
return float32(ir) / 255, float32(ig) / 255, float32(ib) / 255, float32(ia) / 255, true return glColor{r: float32(ir) / 255, g: float32(ig) / 255, b: float32(ib) / 255, a: float32(ia) / 255}, true
} else { } else {
return return
} }
@ -207,11 +211,11 @@ func parseColor(value ...interface{}) (r, g, b, a float32, ok bool) {
var ir, ig, ib, ia int var ir, ig, ib, ia int
n, err := fmt.Sscanf(v, "rgb(%d,%d,%d)", &ir, &ig, &ib) n, err := fmt.Sscanf(v, "rgb(%d,%d,%d)", &ir, &ig, &ib)
if err == nil && n == 3 { if err == nil && n == 3 {
return float32(ir) / 255, float32(ig) / 255, float32(ib) / 255, 1, true return glColor{r: float32(ir) / 255, g: float32(ig) / 255, b: float32(ib) / 255, a: 1}, true
} }
n, err = fmt.Sscanf(v, "rgba(%d,%d,%d,%d)", &ir, &ig, &ib, &ia) n, err = fmt.Sscanf(v, "rgba(%d,%d,%d,%d)", &ir, &ig, &ib, &ia)
if err == nil && n == 4 { if err == nil && n == 4 {
return float32(ir) / 255, float32(ig) / 255, float32(ib) / 255, float32(ia) / 255, true return glColor{r: float32(ir) / 255, g: float32(ig) / 255, b: float32(ib) / 255, a: float32(ia) / 255}, true
} }
} }
} }
@ -237,8 +241,8 @@ func parseColor(value ...interface{}) (r, g, b, a float32, ok bool) {
} else { } else {
pa = 1 pa = 1
} }
return pr, pg, pb, pa, true return glColor{r: pr, g: pg, b: pb, a: pa}, true
} }
return 0, 0, 0, 1, false return glColor{r: 0, g: 0, b: 0, a: 1}, false
} }

View file

@ -166,8 +166,8 @@ func (cv *Canvas) Stroke() {
gli.StencilMask(0x01) gli.StencilMask(0x01)
gli.UseProgram(sr.id) gli.UseProgram(sr.id)
s := cv.state.stroke c := cv.state.stroke.color
gli.Uniform4f(sr.color, s.r, s.g, s.b, s.a) gli.Uniform4f(sr.color, c.r, c.g, c.b, c.a)
gli.EnableVertexAttribArray(sr.vertex) gli.EnableVertexAttribArray(sr.vertex)
gli.BindBuffer(gl_ARRAY_BUFFER, buf) gli.BindBuffer(gl_ARRAY_BUFFER, buf)
@ -353,8 +353,8 @@ func (cv *Canvas) Fill() {
cv.activate() cv.activate()
gli.UseProgram(sr.id) gli.UseProgram(sr.id)
f := cv.state.fill c := cv.state.fill.color
gli.Uniform4f(sr.color, f.r, f.g, f.b, f.a) gli.Uniform4f(sr.color, c.r, c.g, c.b, c.a)
gli.EnableVertexAttribArray(sr.vertex) gli.EnableVertexAttribArray(sr.vertex)
gli.BindBuffer(gl_ARRAY_BUFFER, buf) gli.BindBuffer(gl_ARRAY_BUFFER, buf)
@ -395,8 +395,8 @@ func (cv *Canvas) clip(path []pathPoint) {
gli.Clear(gl_STENCIL_BUFFER_BIT) gli.Clear(gl_STENCIL_BUFFER_BIT)
gli.UseProgram(sr.id) gli.UseProgram(sr.id)
f := cv.state.fill c := cv.state.fill.color
gli.Uniform4f(sr.color, f.r, f.g, f.b, f.a) gli.Uniform4f(sr.color, c.r, c.g, c.b, c.a)
gli.EnableVertexAttribArray(sr.vertex) gli.EnableVertexAttribArray(sr.vertex)
gli.BindBuffer(gl_ARRAY_BUFFER, buf) gli.BindBuffer(gl_ARRAY_BUFFER, buf)

View file

@ -61,8 +61,7 @@ func (cv *Canvas) FillText(str string, x, y float32) {
fontRenderingContext.setFont(cv.state.font.font) fontRenderingContext.setFont(cv.state.font.font)
fontRenderingContext.setFontSize(float64(cv.state.fontSize)) fontRenderingContext.setFontSize(float64(cv.state.fontSize))
f := cv.state.fill fontRenderingContext.setSrc(image.NewUniform(colorGLToGo(cv.state.fill.color)))
fontRenderingContext.setSrc(image.NewUniform(colorGLToGo(f.r, f.g, f.b, f.a)))
fontRenderingContext.setDst(cv.text.target) fontRenderingContext.setDst(cv.text.target)
fontRenderingContext.setClip(cv.text.target.Bounds()) fontRenderingContext.setClip(cv.text.target.Bounds())
_, bounds, _ := fontRenderingContext.drawString(str, fixed.Point26_6{X: fixed.Int26_6(x*64 + 0.5), Y: fixed.Int26_6(y*64 + 0.5)}) _, bounds, _ := fontRenderingContext.drawString(str, fixed.Point26_6{X: fixed.Int26_6(x*64 + 0.5), Y: fixed.Int26_6(y*64 + 0.5)})