2016-07-09 14:23:35 -04:00
|
|
|
package drawing
|
|
|
|
|
|
|
|
import (
|
2023-11-04 16:30:06 -04:00
|
|
|
"fmt"
|
2016-07-09 14:23:35 -04:00
|
|
|
"testing"
|
|
|
|
|
2017-03-05 19:54:40 -05:00
|
|
|
"image/color"
|
|
|
|
|
2020-11-22 19:45:10 -05:00
|
|
|
"github.com/wcharczuk/go-chart/v2/testutil"
|
2016-07-09 14:23:35 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestColorFromHex(t *testing.T) {
|
|
|
|
white := ColorFromHex("FFFFFF")
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, ColorWhite, white)
|
2016-07-09 14:23:35 -04:00
|
|
|
|
|
|
|
shortWhite := ColorFromHex("FFF")
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, ColorWhite, shortWhite)
|
2016-07-09 14:23:35 -04:00
|
|
|
|
|
|
|
black := ColorFromHex("000000")
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, ColorBlack, black)
|
2016-07-09 14:23:35 -04:00
|
|
|
|
|
|
|
shortBlack := ColorFromHex("000")
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, ColorBlack, shortBlack)
|
2016-07-09 14:23:35 -04:00
|
|
|
|
|
|
|
red := ColorFromHex("FF0000")
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, ColorRed, red)
|
2016-07-09 14:23:35 -04:00
|
|
|
|
|
|
|
shortRed := ColorFromHex("F00")
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, ColorRed, shortRed)
|
2016-07-09 14:23:35 -04:00
|
|
|
|
2023-11-04 16:30:06 -04:00
|
|
|
green := ColorFromHex("008000")
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, ColorGreen, green)
|
2016-07-09 14:23:35 -04:00
|
|
|
|
2023-11-04 16:30:06 -04:00
|
|
|
// shortGreen := ColorFromHex("0F0")
|
|
|
|
// testutil.AssertEqual(t, ColorGreen, shortGreen)
|
2016-07-09 14:23:35 -04:00
|
|
|
|
|
|
|
blue := ColorFromHex("0000FF")
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, ColorBlue, blue)
|
2016-07-09 14:23:35 -04:00
|
|
|
|
|
|
|
shortBlue := ColorFromHex("00F")
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertEqual(t, ColorBlue, shortBlue)
|
2016-07-09 14:23:35 -04:00
|
|
|
}
|
2017-03-05 19:54:40 -05:00
|
|
|
|
2023-11-04 12:44:19 -04:00
|
|
|
func TestColorFromHex_handlesHash(t *testing.T) {
|
|
|
|
withHash := ColorFromHex("#FF0000")
|
|
|
|
testutil.AssertEqual(t, ColorRed, withHash)
|
2017-03-05 19:54:40 -05:00
|
|
|
|
2023-11-04 12:44:19 -04:00
|
|
|
withoutHash := ColorFromHex("#FF0000")
|
|
|
|
testutil.AssertEqual(t, ColorRed, withoutHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestColorFromAlphaMixedRGBA(t *testing.T) {
|
2017-03-05 19:54:40 -05:00
|
|
|
black := ColorFromAlphaMixedRGBA(color.Black.RGBA())
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertTrue(t, black.Equals(ColorBlack), black.String())
|
2017-03-05 19:54:40 -05:00
|
|
|
|
|
|
|
white := ColorFromAlphaMixedRGBA(color.White.RGBA())
|
2020-11-22 19:45:10 -05:00
|
|
|
testutil.AssertTrue(t, white.Equals(ColorWhite), white.String())
|
2017-03-05 19:54:40 -05:00
|
|
|
}
|
2023-11-04 16:30:06 -04:00
|
|
|
|
|
|
|
func Test_ColorFromRGBA(t *testing.T) {
|
|
|
|
value := "rgba(192, 192, 192, 1.0)"
|
|
|
|
parsed := ColorFromRGBA(value)
|
|
|
|
testutil.AssertEqual(t, ColorSilver, parsed)
|
|
|
|
|
|
|
|
value = "rgba(192,192,192,1.0)"
|
|
|
|
parsed = ColorFromRGBA(value)
|
|
|
|
testutil.AssertEqual(t, ColorSilver, parsed)
|
|
|
|
|
|
|
|
value = "rgba(192,192,192,1.5)"
|
|
|
|
parsed = ColorFromRGBA(value)
|
|
|
|
testutil.AssertEqual(t, ColorSilver, parsed)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseColor(t *testing.T) {
|
|
|
|
testCases := [...]struct {
|
|
|
|
Input string
|
|
|
|
Expected Color
|
|
|
|
}{
|
|
|
|
{"", Color{}},
|
|
|
|
{"white", ColorWhite},
|
|
|
|
{"WHITE", ColorWhite}, // caps!
|
|
|
|
{"black", ColorBlack},
|
|
|
|
{"red", ColorRed},
|
|
|
|
{"green", ColorGreen},
|
|
|
|
{"blue", ColorBlue},
|
|
|
|
{"silver", ColorSilver},
|
|
|
|
{"maroon", ColorMaroon},
|
|
|
|
{"purple", ColorPurple},
|
|
|
|
{"fuchsia", ColorFuchsia},
|
|
|
|
{"lime", ColorLime},
|
|
|
|
{"olive", ColorOlive},
|
|
|
|
{"yellow", ColorYellow},
|
|
|
|
{"navy", ColorNavy},
|
|
|
|
{"teal", ColorTeal},
|
|
|
|
{"aqua", ColorAqua},
|
|
|
|
|
|
|
|
{"rgba(192, 192, 192, 1.0)", ColorSilver},
|
|
|
|
{"rgba(192,192,192,1.0)", ColorSilver},
|
|
|
|
{"rgb(192, 192, 192)", ColorSilver},
|
|
|
|
{"rgb(192,192,192)", ColorSilver},
|
|
|
|
|
|
|
|
{"#FF0000", ColorRed},
|
|
|
|
{"#008000", ColorGreen},
|
|
|
|
{"#0000FF", ColorBlue},
|
|
|
|
{"#F00", ColorRed},
|
|
|
|
{"#080", Color{0, 136, 0, 255}},
|
|
|
|
{"#00F", ColorBlue},
|
|
|
|
}
|
|
|
|
|
|
|
|
for index, tc := range testCases {
|
|
|
|
actual := ParseColor(tc.Input)
|
|
|
|
testutil.AssertEqual(t, tc.Expected, actual, fmt.Sprintf("test case: %d -> %s", index, tc.Input))
|
|
|
|
}
|
|
|
|
}
|