diff --git a/chart_test.go b/chart_test.go index 87d00ed..02ac80d 100644 --- a/chart_test.go +++ b/chart_test.go @@ -483,3 +483,24 @@ func TestChartCheckRangesWithRanges(t *testing.T) { xr, yr, yra := c.getRanges() 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 ... + + +} diff --git a/drawing/color.go b/drawing/color.go index 19b3a4f..488bdcd 100644 --- a/drawing/color.go +++ b/drawing/color.go @@ -46,12 +46,20 @@ func ColorFromHex(hex string) Color { 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. type Color struct { - R uint8 - G uint8 - B uint8 - A uint8 + R, G, B, A uint8 } // 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. func (c Color) String() string { fa := float64(c.A) / float64(255) diff --git a/drawing/color_test.go b/drawing/color_test.go index d0616e2..bdedd02 100644 --- a/drawing/color_test.go +++ b/drawing/color_test.go @@ -3,6 +3,8 @@ package drawing import ( "testing" + "image/color" + "github.com/blendlabs/go-assert" ) @@ -39,3 +41,13 @@ func TestColorFromHex(t *testing.T) { shortBlue := ColorFromHex("00F") 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()) +} diff --git a/drawing/image.go b/drawing/image.go new file mode 100644 index 0000000..5252fb9 --- /dev/null +++ b/drawing/image.go @@ -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()) +}