Skip to content

Commit

Permalink
More color testing and tweaks to prior commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jentfoo committed Jan 25, 2025
1 parent de68c5a commit baf8168
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 18 deletions.
13 changes: 10 additions & 3 deletions color.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package charts

import (
"image/color"
"math"
"strings"

Expand Down Expand Up @@ -151,7 +152,13 @@ func ColorFromRGBA(color string) Color {
return drawing.ColorFromRGBA(color)
}

// ColorFromRGBAValues returns the system alpha mixed rgba values.
func ColorFromRGBAValues(r, g, b, a uint32) Color {
return drawing.ColorFromAlphaMixedRGBA(r, g, b, a)
// ColorRGB constructs a fully opaque color with the color specified.
func ColorRGB(r, g, b uint8) Color {
return Color{R: r, G: g, B: b, A: 255}
}

// ColorConvertGo converts go's built in colors to our Color struct.
// This allows easy use of colors defined in image/colornames.
func ColorConvertGo(c color.RGBA) Color {
return Color{R: c.R, G: c.G, B: c.B, A: c.A}
}
46 changes: 38 additions & 8 deletions color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/image/colornames"
)

func BenchmarkParseColor(b *testing.B) {
Expand Down Expand Up @@ -39,7 +40,11 @@ func testColorShades(t *testing.T, colors ...Color) {

sampleWidth := p.Width() / len(colors)
for i, c := range colors {
p.FilledRect(i*sampleWidth, 0, (i+1)*sampleWidth, p.Height(),
endX := (i + 1) * sampleWidth
if i == len(colors)-1 {
endX = p.Width() // ensure edge is painted
}
p.FilledRect(i*sampleWidth, 0, endX, p.Height(),
c, ColorTransparent, 0.0)
}

Expand All @@ -49,35 +54,42 @@ func testColorShades(t *testing.T, colors ...Color) {
}

func TestGrayColors(t *testing.T) {
testColorShades(t, ColorDarkGray, ColorGray, ColorLightGray)
testColorShades(t, ColorDarkGray, ColorGray, ColorSilver, ColorLightGray, ColorAzure,
ColorSlateGray, ColorLightSlateGray)
}

func TestBlueColors(t *testing.T) {
testColorShades(t, ColorBlue, ColorBlueAlt1, ColorBlueAlt2)
testColorShades(t, ColorBlue, ColorNavy, ColorBlueAlt1, ColorBlueAlt2, ColorLightSlateGray)
}

func TestGreenColors(t *testing.T) {
testColorShades(t, ColorGreen, ColorGreenAlt1, ColorGreenAlt2, ColorGreenAlt3, ColorGreenAlt4)
testColorShades(t, ColorGreen, ColorOlive, ColorLime,
ColorGreenAlt1, ColorGreenAlt2, ColorGreenAlt3, ColorGreenAlt4)
}

func TestRedColors(t *testing.T) {
testColorShades(t, ColorRed, ColorRedAlt1, ColorRedAlt2)
testColorShades(t, ColorRed, ColorPink, ColorSalmon, ColorMaroon, ColorBrown, ColorChocolate,
ColorRedAlt1, ColorRedAlt2)
}

func TestOrangeColors(t *testing.T) {
testColorShades(t, ColorOrange, ColorOrangeAlt1, ColorOrangeAlt2, ColorOrangeAlt3)
}

func TestAquaColors(t *testing.T) {
testColorShades(t, ColorAqua, ColorAquaAlt1)
testColorShades(t, ColorAqua, ColorTeal, ColorTurquoise, ColorAquaAlt1)
}

func TestYellowColors(t *testing.T) {
testColorShades(t, ColorYellow, ColorYellowAlt1)
testColorShades(t, ColorYellow, ColorGold, ColorYellowAlt1)
}

func TestTanColors(t *testing.T) {
testColorShades(t, ColorAzure, ColorIvory, ColorBeige, ColorKhaki, ColorTan, ColorCoral, ColorSalmon)
}

func TestPurpleColors(t *testing.T) {
testColorShades(t, ColorPurple, ColorViolet, ColorPlum, ColorFuchsia)
testColorShades(t, ColorPurple, ColorViolet, ColorIndigo, ColorPlum, ColorFuchsia)
}

func TestIsLightColor(t *testing.T) {
Expand All @@ -97,6 +109,9 @@ func TestParseColor(t *testing.T) {
c := ParseColor("")
assert.True(t, c.IsZero())

c = ParseColor("unknown")
assert.Equal(t, ColorBlack, c)

c = ParseColor("#333")
assert.Equal(t, Color{R: 51, G: 51, B: 51, A: 255}, c)

Expand All @@ -112,3 +127,18 @@ func TestParseColor(t *testing.T) {
c = ParseColor("rgba(50,51,52,250)")
assert.Equal(t, Color{R: 50, G: 51, B: 52, A: 250}, c)
}

func TestColorConvertGo(t *testing.T) {
t.Parallel()

goC := colornames.Lavender
ourC := ColorConvertGo(goC)

goR, goG, goB, goA := goC.RGBA()
ourR, ourG, ourB, ourA := ourC.RGBA()

assert.Equal(t, goR, ourR)
assert.Equal(t, goG, ourG)
assert.Equal(t, goB, ourB)
assert.Equal(t, goA, ourA)
}
8 changes: 4 additions & 4 deletions examples/table-1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ func main() {
panic(err)
}

bgColor := charts.Color{R: 28, G: 28, B: 32, A: 255}
bgColor := charts.ColorRGB(28, 28, 32)
p, err = charts.TableOptionRenderDirect(charts.TableChartOption{
Header: []string{"Name", "Price", "Change"},
BackgroundColor: bgColor,
HeaderBackgroundColor: charts.Color{R: 80, G: 80, B: 80, A: 255},
HeaderBackgroundColor: charts.ColorRGB(80, 80, 80),
RowBackgroundColors: []charts.Color{bgColor},
HeaderFontColor: charts.ColorWhite,
FontStyle: charts.FontStyle{
Expand Down Expand Up @@ -127,9 +127,9 @@ func main() {
}

if value > 0 {
tc.FillColor = charts.Color{R: 179, G: 53, B: 20, A: 255}
tc.FillColor = charts.ColorRGB(179, 53, 20)
} else {
tc.FillColor = charts.Color{R: 33, G: 124, B: 50, A: 255}
tc.FillColor = charts.ColorRGB(33, 124, 50)
}
return tc
},
Expand Down
4 changes: 2 additions & 2 deletions examples/web-1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,11 +378,11 @@ func indexHandler(w http.ResponseWriter, req *http.Request) {
YAxis: []charts.YAxisOption{
{
Formatter: "{value}ml",
AxisColor: charts.Color{R: 84, G: 112, B: 198, A: 255},
AxisColor: charts.ColorRGB(84, 112, 198),
},
{
Formatter: "{value}°C",
AxisColor: charts.Color{R: 250, G: 200, B: 88, A: 255},
AxisColor: charts.ColorRGB(250, 200, 88),
},
},
SeriesList: append(charts.NewSeriesListBar([][]float64{
Expand Down
2 changes: 1 addition & 1 deletion painter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func TestPainterExternal(t *testing.T) {
_ = p.LineChart(opt)
p = p.Child(PainterBoxOption(chartdraw.NewBox(0, 200, 400, 200)))
opt = makeMinimalLineChartOption()
opt.Theme = GetDefaultTheme().WithBackgroundColor(ColorFromRGBAValues(0, 0, 0, 0))
opt.Theme = GetDefaultTheme().WithBackgroundColor(ColorTransparent)
_ = p.LineChart(opt)
},
result: "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" viewBox=\"0 0 400 300\"><path d=\"M 5 10\nL 400 10\nL 400 300\nL 5 300\nL 5 10\" style=\"stroke:none;fill:white\"/><text x=\"15\" y=\"27\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">1440</text><text x=\"15\" y=\"53\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">1280</text><text x=\"15\" y=\"80\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">1120</text><text x=\"23\" y=\"107\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">960</text><text x=\"23\" y=\"133\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">800</text><text x=\"23\" y=\"160\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">640</text><text x=\"23\" y=\"187\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">480</text><text x=\"23\" y=\"213\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">320</text><text x=\"23\" y=\"240\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">160</text><text x=\"41\" y=\"267\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">0</text><path d=\"M 60 20\nL 390 20\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 60 46\nL 390 46\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 60 73\nL 390 73\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 60 100\nL 390 100\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 60 126\nL 390 126\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 60 153\nL 390 153\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 60 180\nL 390 180\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 60 206\nL 390 206\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 60 233\nL 390 233\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 83 240\nL 130 238\nL 177 244\nL 224 238\nL 271 245\nL 318 222\nL 366 225\" style=\"stroke-width:2;stroke:rgb(84,112,198);fill:none\"/><path d=\"M 83 124\nL 130 105\nL 177 110\nL 224 105\nL 271 45\nL 318 39\nL 366 40\" style=\"stroke-width:2;stroke:rgb(145,204,117);fill:none\"/><path d=\"M 200 0\nL 400 0\nL 400 200\nL 200 200\nL 200 0\" style=\"stroke:none;fill:none\"/><text x=\"210\" y=\"17\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">1.44k</text><text x=\"210\" y=\"33\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">1.28k</text><text x=\"210\" y=\"50\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">1.12k</text><text x=\"222\" y=\"67\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">960</text><text x=\"222\" y=\"83\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">800</text><text x=\"222\" y=\"100\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">640</text><text x=\"222\" y=\"117\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">480</text><text x=\"222\" y=\"133\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">320</text><text x=\"222\" y=\"150\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">160</text><text x=\"240\" y=\"167\" style=\"stroke:none;fill:rgb(70,70,70);font-size:15.3px;font-family:'Roboto Medium',sans-serif\">0</text><path d=\"M 259 10\nL 390 10\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 259 26\nL 390 26\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 259 43\nL 390 43\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 259 60\nL 390 60\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 259 76\nL 390 76\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 259 93\nL 390 93\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 259 110\nL 390 110\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 259 126\nL 390 126\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 259 143\nL 390 143\" style=\"stroke-width:1;stroke:rgb(224,230,242);fill:none\"/><path d=\"M 259 148\nL 280 147\nL 302 150\nL 324 147\nL 346 151\nL 368 137\nL 390 139\" style=\"stroke-width:2;stroke:rgb(84,112,198);fill:none\"/><path d=\"M 259 75\nL 280 63\nL 302 67\nL 324 63\nL 346 26\nL 368 22\nL 390 23\" style=\"stroke-width:2;stroke:rgb(145,204,117);fill:none\"/></svg>",
Expand Down

0 comments on commit baf8168

Please sign in to comment.