diff --git a/excelize_test.go b/excelize_test.go index 9c1b1e629944c1d3f0c92fa27f4c99ca9ee73c34..b0483c75218af6f88e4ae7438e350e52d67ba50a 100644 --- a/excelize_test.go +++ b/excelize_test.go @@ -1110,26 +1110,33 @@ func TestSetSheetRow(t *testing.T) { assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetSheetRow.xlsx"))) } -func TestThemeColor(t *testing.T) { - t.Log(ThemeColor("000000", -0.1)) - t.Log(ThemeColor("000000", 0)) - t.Log(ThemeColor("000000", 1)) -} - func TestHSL(t *testing.T) { var hsl HSL - t.Log(hsl.RGBA()) - t.Log(hslModel(hsl)) - t.Log(hslModel(color.Gray16{Y: uint16(1)})) - t.Log(HSLToRGB(0, 1, 0.4)) - t.Log(HSLToRGB(0, 1, 0.6)) - t.Log(hueToRGB(0, 0, -1)) - t.Log(hueToRGB(0, 0, 2)) - t.Log(hueToRGB(0, 0, 1.0/7)) - t.Log(hueToRGB(0, 0, 0.4)) - t.Log(hueToRGB(0, 0, 2.0/4)) + r, g, b, a := hsl.RGBA() + assert.Equal(t, uint32(0), r) + assert.Equal(t, uint32(0), g) + assert.Equal(t, uint32(0), b) + assert.Equal(t, uint32(0xffff), a) + assert.Equal(t, HSL{0, 0, 0}, hslModel(hsl)) + assert.Equal(t, HSL{0, 0, 0}, hslModel(color.Gray16{Y: uint16(1)})) + R, G, B := HSLToRGB(0, 1, 0.4) + assert.Equal(t, uint8(204), R) + assert.Equal(t, uint8(0), G) + assert.Equal(t, uint8(0), B) + R, G, B = HSLToRGB(0, 1, 0.6) + assert.Equal(t, uint8(255), R) + assert.Equal(t, uint8(51), G) + assert.Equal(t, uint8(51), B) + assert.Equal(t, 0.0, hueToRGB(0, 0, -1)) + assert.Equal(t, 0.0, hueToRGB(0, 0, 2)) + assert.Equal(t, 0.0, hueToRGB(0, 0, 1.0/7)) + assert.Equal(t, 0.0, hueToRGB(0, 0, 0.4)) + assert.Equal(t, 0.0, hueToRGB(0, 0, 2.0/4)) t.Log(RGBToHSL(255, 255, 0)) - t.Log(RGBToHSL(0, 255, 255)) + h, s, l := RGBToHSL(0, 255, 255) + assert.Equal(t, float64(0.5), h) + assert.Equal(t, float64(1), s) + assert.Equal(t, float64(0.5), l) t.Log(RGBToHSL(250, 100, 50)) t.Log(RGBToHSL(50, 100, 250)) t.Log(RGBToHSL(250, 50, 100)) diff --git a/styles.go b/styles.go index 896eaa1bf68cf25b1bd2f917f50d4022ee8b5bac..decc89bd5918a58c5ebe7c9ef419558aebfe3966 100644 --- a/styles.go +++ b/styles.go @@ -3110,12 +3110,10 @@ func (f *File) themeReader() *xlsxTheme { err error theme xlsxTheme ) - if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML("xl/theme/theme1.xml")))). Decode(&theme); err != nil && err != io.EOF { log.Printf("xml decoder error: %s", err) } - return &theme } @@ -3127,7 +3125,10 @@ func ThemeColor(baseColor string, tint float64) string { r, _ := strconv.ParseInt(baseColor[0:2], 16, 64) g, _ := strconv.ParseInt(baseColor[2:4], 16, 64) b, _ := strconv.ParseInt(baseColor[4:6], 16, 64) - h, s, l := RGBToHSL(uint8(r), uint8(g), uint8(b)) + var h, s, l float64 + if r >= 0 && r <= math.MaxUint8 && g >= 0 && g <= math.MaxUint8 && b >= 0 && b <= math.MaxUint8 { + h, s, l = RGBToHSL(uint8(r), uint8(g), uint8(b)) + } if tint < 0 { l *= (1 + tint) } else { diff --git a/styles_test.go b/styles_test.go index 8ce26a4e7bf4b2163a02fd3daf90a707477a80c7..e93aa708a1d1e3065491fc937cf6388e82985fa3 100644 --- a/styles_test.go +++ b/styles_test.go @@ -2,6 +2,7 @@ package excelize import ( "fmt" + "math" "path/filepath" "strings" "testing" @@ -294,5 +295,17 @@ func TestParseTime(t *testing.T) { assert.Equal(t, "3/4/2019 5:5:42", parseTime("43528.2123", "M/D/YYYY h:m:s")) assert.Equal(t, "March", parseTime("43528", "mmmm")) assert.Equal(t, "Monday", parseTime("43528", "dddd")) +} +func TestThemeColor(t *testing.T) { + for _, clr := range [][]string{ + {"FF000000", ThemeColor("000000", -0.1)}, + {"FF000000", ThemeColor("000000", 0)}, + {"FF33FF33", ThemeColor("00FF00", 0.2)}, + {"FFFFFFFF", ThemeColor("000000", 1)}, + {"FFFFFFFF", ThemeColor(strings.Repeat(string(rune(math.MaxUint8+1)), 6), 1)}, + {"FFFFFFFF", ThemeColor(strings.Repeat(string(rune(-1)), 6), 1)}, + } { + assert.Equal(t, clr[0], clr[1]) + } }