color tests etc.

This commit is contained in:
Will Charczuk 2017-03-05 16:23:11 -08:00
parent d9b5269579
commit 10950a3bf2
4 changed files with 76 additions and 4 deletions

View file

@ -483,3 +483,24 @@ func TestChartCheckRangesWithRanges(t *testing.T) {
xr, yr, yra := c.getRanges() xr, yr, yra := c.getRanges()
assert.Nil(c.checkRanges(xr, yr, yra)) assert.Nil(c.checkRanges(xr, yr, yra))
} }
func TestChartE2ELine(t *testing.T) {
assert := assert.New(t)
c := Chart{
Series: []Series{
ContinuousSeries{
XValues: Sequence.Float64(0, 4, 1),
YValues: Sequence.Float64(0, 4, 1),
},
},
}
var buffer = &bytes.Buffer{}
err := c.Render(PNG, buffer)
assert.Nil(err)
// do color tests ...
}

View file

@ -46,12 +46,20 @@ func ColorFromHex(hex string) Color {
return c return c
} }
// ColorFromAlphaMixedRGBA returns the system alpha mixed rgba values.
func ColorFromAlphaMixedRGBA(r, g, b, a uint32) Color {
fa := float64(a) / 255.0
var c Color
c.R = uint8(float64(r) / fa)
c.G = uint8(float64(g) / fa)
c.B = uint8(float64(b) / fa)
c.A = uint8(a | (a >> 8))
return c
}
// Color is our internal color type because color.Color is bullshit. // Color is our internal color type because color.Color is bullshit.
type Color struct { type Color struct {
R uint8 R, G, B, A uint8
G uint8
B uint8
A uint8
} }
// RGBA returns the color as a pre-alpha mixed color set. // RGBA returns the color as a pre-alpha mixed color set.
@ -88,6 +96,14 @@ func (c Color) WithAlpha(a uint8) Color {
} }
} }
// Equals returns true if the color equals another.
func (c Color) Equals(other Color) bool {
return c.R == other.R &&
c.G == other.G &&
c.B == other.B &&
c.A == other.A
}
// String returns a css string representation of the color. // String returns a css string representation of the color.
func (c Color) String() string { func (c Color) String() string {
fa := float64(c.A) / float64(255) fa := float64(c.A) / float64(255)

View file

@ -3,6 +3,8 @@ package drawing
import ( import (
"testing" "testing"
"image/color"
"github.com/blendlabs/go-assert" "github.com/blendlabs/go-assert"
) )
@ -39,3 +41,13 @@ func TestColorFromHex(t *testing.T) {
shortBlue := ColorFromHex("00F") shortBlue := ColorFromHex("00F")
assert.Equal(ColorBlue, shortBlue) assert.Equal(ColorBlue, shortBlue)
} }
func TestColorFromAlphaMixedRGBA(t *testing.T) {
assert := assert.New(t)
black := ColorFromAlphaMixedRGBA(color.Black.RGBA())
assert.True(black.Equals(ColorBlack), black.String())
white := ColorFromAlphaMixedRGBA(color.White.RGBA())
assert.True(white.Equals(ColorWhite), white.String())
}

23
drawing/image.go Normal file
View file

@ -0,0 +1,23 @@
package drawing
import "image"
// Image is a helper wraper that allows (sane) access to pixel info.
type Image struct {
Inner *image.RGBA
}
// Width returns the image's width in pixels.
func (i Image) Width() int {
return i.Inner.Rect.Size().X
}
// Height returns the image's height in pixels.
func (i Image) Height() int {
return i.Inner.Rect.Size().Y
}
// At returns a pixel color at a given x/y.
func (i Image) At(x, y int) Color {
return ColorFromAlphaMixedRGBA(i.Inner.At(x, y).RGBA())
}