This close #1963, prevent the GetStyle function panic when theme without sysClr

This commit is contained in:
xuri 2024-07-23 21:48:17 +08:00
parent d81b4c8661
commit 30c4cd70e0
No known key found for this signature in database
GPG Key ID: BA5E5BB1C948EDF7
3 changed files with 24 additions and 11 deletions

View File

@ -1376,22 +1376,33 @@ var (
} }
) )
// colorChoice returns a hex color code from the actual color values.
func (clr *decodeCTColor) colorChoice() *string {
if clr.SrgbClr != nil {
return clr.SrgbClr.Val
}
if clr.SysClr != nil {
return &clr.SysClr.LastClr
}
return nil
}
// GetBaseColor returns the preferred hex color code by giving hex color code, // GetBaseColor returns the preferred hex color code by giving hex color code,
// indexed color, and theme color. // indexed color, and theme color.
func (f *File) GetBaseColor(hexColor string, indexedColor int, themeColor *int) string { func (f *File) GetBaseColor(hexColor string, indexedColor int, themeColor *int) string {
if f.Theme != nil && themeColor != nil { if f.Theme != nil && themeColor != nil {
clrScheme := f.Theme.ThemeElements.ClrScheme clrScheme := f.Theme.ThemeElements.ClrScheme
if val, ok := map[int]*string{ if val, ok := map[int]*string{
0: &clrScheme.Lt1.SysClr.LastClr, 0: clrScheme.Lt1.colorChoice(),
1: &clrScheme.Dk1.SysClr.LastClr, 1: clrScheme.Dk1.colorChoice(),
2: clrScheme.Lt2.SrgbClr.Val, 2: clrScheme.Lt2.colorChoice(),
3: clrScheme.Dk2.SrgbClr.Val, 3: clrScheme.Dk2.colorChoice(),
4: clrScheme.Accent1.SrgbClr.Val, 4: clrScheme.Accent1.colorChoice(),
5: clrScheme.Accent2.SrgbClr.Val, 5: clrScheme.Accent2.colorChoice(),
6: clrScheme.Accent3.SrgbClr.Val, 6: clrScheme.Accent3.colorChoice(),
7: clrScheme.Accent4.SrgbClr.Val, 7: clrScheme.Accent4.colorChoice(),
8: clrScheme.Accent5.SrgbClr.Val, 8: clrScheme.Accent5.colorChoice(),
9: clrScheme.Accent6.SrgbClr.Val, 9: clrScheme.Accent6.colorChoice(),
}[*themeColor]; ok && val != nil { }[*themeColor]; ok && val != nil {
return *val return *val
} }

View File

@ -613,6 +613,8 @@ func TestGetThemeColor(t *testing.T) {
assert.Equal(t, "FFFFFF", f.getThemeColor(&xlsxColor{RGB: "FFFFFF"})) assert.Equal(t, "FFFFFF", f.getThemeColor(&xlsxColor{RGB: "FFFFFF"}))
assert.Equal(t, "FF8080", f.getThemeColor(&xlsxColor{Indexed: 2, Tint: 0.5})) assert.Equal(t, "FF8080", f.getThemeColor(&xlsxColor{Indexed: 2, Tint: 0.5}))
assert.Empty(t, f.getThemeColor(&xlsxColor{Indexed: len(IndexedColorMapping), Tint: 0.5})) assert.Empty(t, f.getThemeColor(&xlsxColor{Indexed: len(IndexedColorMapping), Tint: 0.5}))
clr := &decodeCTColor{}
assert.Nil(t, clr.colorChoice())
} }
func TestGetStyle(t *testing.T) { func TestGetStyle(t *testing.T) {