Browse Source

Update conversion between integer types and unit tests

xuri 5 years ago
parent
commit
9d470bb38f
3 changed files with 41 additions and 20 deletions
  1. 24 17
      excelize_test.go
  2. 4 3
      styles.go
  3. 13 0
      styles_test.go

+ 24 - 17
excelize_test.go

@@ -1110,26 +1110,33 @@ func TestSetSheetRow(t *testing.T) {
 	assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetSheetRow.xlsx")))
 	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) {
 func TestHSL(t *testing.T) {
 	var hsl HSL
 	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(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(250, 100, 50))
 	t.Log(RGBToHSL(50, 100, 250))
 	t.Log(RGBToHSL(50, 100, 250))
 	t.Log(RGBToHSL(250, 50, 100))
 	t.Log(RGBToHSL(250, 50, 100))

+ 4 - 3
styles.go

@@ -3110,12 +3110,10 @@ func (f *File) themeReader() *xlsxTheme {
 		err   error
 		err   error
 		theme xlsxTheme
 		theme xlsxTheme
 	)
 	)
-
 	if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML("xl/theme/theme1.xml")))).
 	if err = f.xmlNewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML("xl/theme/theme1.xml")))).
 		Decode(&theme); err != nil && err != io.EOF {
 		Decode(&theme); err != nil && err != io.EOF {
 		log.Printf("xml decoder error: %s", err)
 		log.Printf("xml decoder error: %s", err)
 	}
 	}
-
 	return &theme
 	return &theme
 }
 }
 
 
@@ -3127,7 +3125,10 @@ func ThemeColor(baseColor string, tint float64) string {
 	r, _ := strconv.ParseInt(baseColor[0:2], 16, 64)
 	r, _ := strconv.ParseInt(baseColor[0:2], 16, 64)
 	g, _ := strconv.ParseInt(baseColor[2:4], 16, 64)
 	g, _ := strconv.ParseInt(baseColor[2:4], 16, 64)
 	b, _ := strconv.ParseInt(baseColor[4:6], 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 {
 	if tint < 0 {
 		l *= (1 + tint)
 		l *= (1 + tint)
 	} else {
 	} else {

+ 13 - 0
styles_test.go

@@ -2,6 +2,7 @@ package excelize
 
 
 import (
 import (
 	"fmt"
 	"fmt"
+	"math"
 	"path/filepath"
 	"path/filepath"
 	"strings"
 	"strings"
 	"testing"
 	"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, "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, "March", parseTime("43528", "mmmm"))
 	assert.Equal(t, "Monday", parseTime("43528", "dddd"))
 	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])
+	}
 }
 }