Skip to content

Commit

Permalink
Update color API to color.Color from color.RGBA
Browse files Browse the repository at this point in the history
This stops the driver implementation leaking into the API design
  • Loading branch information
andydotxyz committed Nov 4, 2018
1 parent b352f55 commit c626130
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 53 deletions.
8 changes: 4 additions & 4 deletions canvas/circle.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ type Circle struct {
Position2 fyne.Position // The current bottomright position of the Circle
Options Options // Options to pass to the renderer

FillColor color.RGBA // The circle fill colour
StrokeColor color.RGBA // The circle stroke colour
StrokeWidth float32 // The stroke width of the circle
FillColor color.Color // The circle fill colour
StrokeColor color.Color // The circle stroke colour
StrokeWidth float32 // The stroke width of the circle
}

// CurrentSize returns the current size of bounding box for this circle object
Expand Down Expand Up @@ -44,7 +44,7 @@ func (l *Circle) MinSize() fyne.Size {
}

// NewCircle returns a new Circle instance
func NewCircle(color color.RGBA) *Circle {
func NewCircle(color color.Color) *Circle {
return &Circle{
FillColor: color,
}
Expand Down
11 changes: 5 additions & 6 deletions canvas/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ type Image struct {
baseObject

// one of the following sources will provide our image data
PixelColor func(x, y, w, h int) color.RGBA // Render the image from code
File string // Load the image froma file
PixelColor func(x, y, w, h int) color.Color // Render the image from code
File string // Load the image froma file

Translucency float64 // Set a translucency value > 0.0 to fade the image
}
Expand All @@ -26,7 +26,7 @@ func (i *Image) Alpha() float64 {
// the specified pixelColor function.
// Images returned from this method should draw dynamically to fill the width
// and height parameters passed to pixelColor.
func NewRaster(pixelColor func(x, y, w, h int) color.RGBA) *Image {
func NewRaster(pixelColor func(x, y, w, h int) color.Color) *Image {
return &Image{
PixelColor: pixelColor,
}
Expand All @@ -38,9 +38,8 @@ func NewRaster(pixelColor func(x, y, w, h int) color.RGBA) *Image {
// starting img.Bounds().Min pixels from the top left of the canvas object.
func NewImageFromImage(img image.Image) *Image {
return &Image{
PixelColor: func(x, y, w, h int) color.RGBA {
r, g, b, a := img.At(x, y).RGBA()
return color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)}
PixelColor: func(x, y, w, h int) color.Color {
return img.At(x, y)
},
}
}
Expand Down
10 changes: 7 additions & 3 deletions canvas/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ func TestImageFromImage(t *testing.T) {
source := image.Rect(2, 2, 4, 4)
dest := NewImageFromImage(source)

assert.Equal(t, dest.PixelColor(0, 0, 6, 6).A, uint8(0x00))
assert.Equal(t, dest.PixelColor(2, 2, 6, 6).A, uint8(0xff))
assert.Equal(t, dest.PixelColor(4, 4, 6, 6).A, uint8(0x00))
// image.Rect is a 16 bit colour model
_, _, _, a := dest.PixelColor(0, 0, 6, 6).RGBA()
assert.Equal(t, uint32(0x0000), a)
_, _, _, a = dest.PixelColor(2, 2, 6, 6).RGBA()
assert.Equal(t, uint32(0xffff), a)
_, _, _, a = dest.PixelColor(4, 4, 6, 6).RGBA()
assert.Equal(t, uint32(0x0000), a)
}

func TestImage_AlphaDefault(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions canvas/line.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type Line struct {
Position2 fyne.Position // The current bottomright position of the Line
Options Options // Options to pass to the renderer

StrokeColor color.RGBA // The line stroke colour
StrokeWidth float32 // The stroke width of the line
StrokeColor color.Color // The line stroke colour
StrokeWidth float32 // The stroke width of the line
}

// CurrentSize returns the current size of bounding box for this line object
Expand Down Expand Up @@ -47,7 +47,7 @@ func (l *Line) MinSize() fyne.Size {
}

// NewLine returns a new Line instance
func NewLine(color color.RGBA) *Line {
func NewLine(color color.Color) *Line {
return &Line{
StrokeColor: color,
StrokeWidth: 1,
Expand Down
8 changes: 4 additions & 4 deletions canvas/rectangle.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import "image/color"
type Rectangle struct {
baseObject

FillColor color.RGBA // The rectangle fill colour
StrokeColor color.RGBA // The rectangle stroke colour
StrokeWidth float32 // The stroke width of the rectangle
FillColor color.Color // The rectangle fill colour
StrokeColor color.Color // The rectangle stroke colour
StrokeWidth float32 // The stroke width of the rectangle
}

// NewRectangle returns a new Rectangle instance
func NewRectangle(color color.RGBA) *Rectangle {
func NewRectangle(color color.Color) *Rectangle {
return &Rectangle{
FillColor: color,
}
Expand Down
4 changes: 2 additions & 2 deletions canvas/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Text struct {
baseObject
Alignment fyne.TextAlign // The alignment of the text content

Color color.RGBA // The main text draw colour
Color color.Color // The main text draw colour
Text string // The string content of this Text
TextSize int // Size of the text - if the Canvas scale is 1.0 this will be equivalent to point size
TextStyle fyne.TextStyle // The style of the text content
Expand Down Expand Up @@ -45,7 +45,7 @@ func (t *Text) MinSize() fyne.Size {
}

// NewText returns a new Text implementation
func NewText(text string, color color.RGBA) *Text {
func NewText(text string, color color.Color) *Text {
return &Text{
Color: color,
Text: text,
Expand Down
46 changes: 24 additions & 22 deletions desktop/canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package desktop
import "C"

import "log"
import "image/color"
import "math"
import "sync"
import "unsafe"
Expand Down Expand Up @@ -72,6 +73,12 @@ func ignoreObject(o fyne.CanvasObject) bool {
return false
}

func setColor(obj *C.Evas_Object, col color.Color) {
r, g, b, a := col.RGBA()

C.evas_object_color_set(obj, C.int((uint8)(r)), C.int((uint8)(g)), C.int((uint8)(b)), C.int((uint8)(a)))
}

func (c *eflCanvas) buildObject(o fyne.CanvasObject, target fyne.CanvasObject, offset fyne.Position) *C.Evas_Object {
var obj *C.Evas_Object
var opts canvas.Options
Expand All @@ -83,16 +90,14 @@ func (c *eflCanvas) buildObject(o fyne.CanvasObject, target fyne.CanvasObject, o
cstr := C.CString(co.Text)
C.evas_object_text_text_set(obj, cstr)
C.free(unsafe.Pointer(cstr))
C.evas_object_color_set(obj, C.int(co.Color.R), C.int(co.Color.G),
C.int(co.Color.B), C.int(co.Color.A))
setColor(obj, co.Color)

updateFont(obj, c, co.TextSize, co.TextStyle)
opts = co.Options
case *canvas.Rectangle:
obj = C.evas_object_rectangle_add(c.evas)

C.evas_object_color_set(obj, C.int(co.FillColor.R), C.int(co.FillColor.G),
C.int(co.FillColor.B), C.int(co.FillColor.A))
setColor(obj, co.FillColor)
opts = co.Options
case *canvas.Image:
obj = C.evas_object_image_filled_add(c.evas)
Expand All @@ -107,15 +112,13 @@ func (c *eflCanvas) buildObject(o fyne.CanvasObject, target fyne.CanvasObject, o
case *canvas.Line:
obj = C.evas_object_line_add(c.evas)

C.evas_object_color_set(obj, C.int(co.StrokeColor.R), C.int(co.StrokeColor.G),
C.int(co.StrokeColor.B), C.int(co.StrokeColor.A))
setColor(obj, co.StrokeColor)
opts = co.Options
case *canvas.Circle:
// TODO - this isnt all there yet, but at least this stops lots of debug output
obj = C.evas_object_rectangle_add(c.evas)

C.evas_object_color_set(obj, C.int(co.StrokeColor.R), C.int(co.StrokeColor.G),
C.int(co.StrokeColor.B), C.int(co.StrokeColor.A))
setColor(obj, co.FillColor)
opts = co.Options
default:
log.Printf("Unrecognised Object %#v\n", o)
Expand All @@ -140,8 +143,8 @@ func (c *eflCanvas) buildContainer(parent fyne.CanvasObject, target fyne.CanvasO
size fyne.Size, pos, offset fyne.Position) {

obj := C.evas_object_rectangle_add(c.evas)
bg := theme.BackgroundColor()
C.evas_object_color_set(obj, C.int(bg.R), C.int(bg.G), C.int(bg.B), C.int(bg.A))
r, g, b, a := theme.BackgroundColor().RGBA()
C.evas_object_color_set(obj, C.int(r), C.int(g), C.int(b), C.int(a))

C.evas_object_show(obj)
c.native[parent] = obj
Expand Down Expand Up @@ -187,9 +190,12 @@ func renderImagePortion(img *canvas.Image, pixels []uint32, wg *sync.WaitGroup,
i := startx + starty*imgWidth
for y := starty; y < starty+height; y++ {
for x := startx; x < startx+width; x++ {
color := img.PixelColor(x, y, imgWidth, imgHeight)
pixels[i] = (uint32)(((uint32)(color.A) << 24) | ((uint32)(color.R) << 16) |
((uint32)(color.G) << 8) | (uint32)(color.B))
col := img.PixelColor(x, y, imgWidth, imgHeight)
// TODO support other color models
if rgba, ok := col.(color.RGBA); ok {
pixels[i] = (uint32)(((uint32)(rgba.A) << 24) | ((uint32)(rgba.R) << 16) |
((uint32)(rgba.G) << 8) | (uint32)(rgba.B))
}
i++
}
i += imgWidth - width
Expand Down Expand Up @@ -249,17 +255,15 @@ func (c *eflCanvas) refreshObject(o, o2 fyne.CanvasObject) {
cstr := C.CString(co.Text)
C.evas_object_text_text_set(obj, cstr)
C.free(unsafe.Pointer(cstr))
C.evas_object_color_set(obj, C.int(co.Color.R), C.int(co.Color.G),
C.int(co.Color.B), C.int(co.Color.A))

setColor(obj, co.Color)
updateFont(obj, c, co.TextSize, co.TextStyle)
pos = getTextPosition(co, pos, size)

C.evas_object_geometry_set(obj, C.Evas_Coord(scaleInt(c, pos.X)), C.Evas_Coord(scaleInt(c, pos.Y)),
C.Evas_Coord(scaleInt(c, size.Width)), C.Evas_Coord(scaleInt(c, size.Height)))
case *canvas.Rectangle:
C.evas_object_color_set(obj, C.int(co.FillColor.R), C.int(co.FillColor.G),
C.int(co.FillColor.B), C.int(co.FillColor.A))
setColor(obj, co.FillColor)
C.evas_object_geometry_set(obj, C.Evas_Coord(scaleInt(c, pos.X)), C.Evas_Coord(scaleInt(c, pos.Y)),
C.Evas_Coord(scaleInt(c, size.Width)), C.Evas_Coord(scaleInt(c, size.Height)))
case *canvas.Image:
Expand Down Expand Up @@ -287,8 +291,7 @@ func (c *eflCanvas) refreshObject(o, o2 fyne.CanvasObject) {
width := co.Position2.X - co.Position1.X
height := co.Position2.Y - co.Position1.Y

C.evas_object_color_set(obj, C.int(co.StrokeColor.R), C.int(co.StrokeColor.G),
C.int(co.StrokeColor.B), C.int(co.StrokeColor.A))
setColor(obj, co.StrokeColor)

if width >= 0 {
if height >= 0 {
Expand All @@ -310,8 +313,7 @@ func (c *eflCanvas) refreshObject(o, o2 fyne.CanvasObject) {
C.evas_object_geometry_set(obj, C.Evas_Coord(scaleInt(c, pos.X)), C.Evas_Coord(scaleInt(c, pos.Y)),
C.Evas_Coord(scaleInt(c, int(math.Abs(float64(width))))+1), C.Evas_Coord(scaleInt(c, int(math.Abs(float64(height))))+1))
case *canvas.Circle:
C.evas_object_color_set(obj, C.int(co.FillColor.R), C.int(co.FillColor.G),
C.int(co.FillColor.B), C.int(co.FillColor.A))
setColor(obj, co.FillColor)
C.evas_object_geometry_set(obj, C.Evas_Coord(scaleInt(c, pos.X)), C.Evas_Coord(scaleInt(c, pos.Y)),
C.Evas_Coord(scaleInt(c, size.Width)), C.Evas_Coord(scaleInt(c, size.Height)))
default:
Expand All @@ -328,7 +330,7 @@ func (c *eflCanvas) refreshContainer(objs []fyne.CanvasObject, target fyne.Canva
if _, ok := target.(*widget.Toolbar); ok { // TODO don't make this a special case
bg = theme.ButtonColor()
}
C.evas_object_color_set(obj, C.int(bg.R), C.int(bg.G), C.int(bg.B), C.int(bg.A))
setColor(obj, bg)

if target == c.content {
C.evas_object_geometry_set(obj, C.Evas_Coord(scaleInt(c, 0)), C.Evas_Coord(scaleInt(c, 0)),
Expand Down
2 changes: 1 addition & 1 deletion examples/apps/canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import "github.com/fyne-io/fyne/canvas"
import "github.com/fyne-io/fyne/layout"
import "github.com/fyne-io/fyne/theme"

func rgbGradient(x, y, w, h int) color.RGBA {
func rgbGradient(x, y, w, h int) color.Color {
g := int(float32(x) / float32(w) * float32(255))
b := int(float32(y) / float32(h) * float32(255))

Expand Down
2 changes: 1 addition & 1 deletion examples/apps/icons.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func Icons(app fyne.App) {
win.Show()
}

func checkerPattern(x, y, _, _ int) color.RGBA {
func checkerPattern(x, y, _, _ int) color.Color {
x /= 20
y /= 20

Expand Down
14 changes: 7 additions & 7 deletions theme/theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ var loadedColors *themeColors
var loadedTheme string

type themeColors struct {
Background color.RGBA
Background color.Color

Button, Text, Primary color.RGBA
Button, Text, Primary color.Color
}

// Basic definition of light theme colours
Expand Down Expand Up @@ -55,27 +55,27 @@ func colors() *themeColors {
}

// BackgroundColor returns the theme's background colour
func BackgroundColor() color.RGBA {
func BackgroundColor() color.Color {
return colors().Background
}

// ButtonColor returns the theme's standard button colour
func ButtonColor() color.RGBA {
func ButtonColor() color.Color {
return colors().Button
}

// TextColor returns the theme's standard text colour
func TextColor() color.RGBA {
func TextColor() color.Color {
return colors().Text
}

// PrimaryColor returns the colour used to highlight primary features
func PrimaryColor() color.RGBA {
func PrimaryColor() color.Color {
return colors().Primary
}

// FocusColor returns the colour used to highlight a focussed widget
func FocusColor() color.RGBA {
func FocusColor() color.Color {
return colors().Primary
}

Expand Down

0 comments on commit c626130

Please sign in to comment.