Skip to content

Commit

Permalink
This close qax-os#1963, prevent the GetStyle function panic when them…
Browse files Browse the repository at this point in the history
…e without sysClr
  • Loading branch information
xuri committed Jul 23, 2024
1 parent d81b4c8 commit 30c4cd7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
31 changes: 21 additions & 10 deletions styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
// indexed color, and theme color.
func (f *File) GetBaseColor(hexColor string, indexedColor int, themeColor *int) string {
if f.Theme != nil && themeColor != nil {
clrScheme := f.Theme.ThemeElements.ClrScheme
if val, ok := map[int]*string{
0: &clrScheme.Lt1.SysClr.LastClr,
1: &clrScheme.Dk1.SysClr.LastClr,
2: clrScheme.Lt2.SrgbClr.Val,
3: clrScheme.Dk2.SrgbClr.Val,
4: clrScheme.Accent1.SrgbClr.Val,
5: clrScheme.Accent2.SrgbClr.Val,
6: clrScheme.Accent3.SrgbClr.Val,
7: clrScheme.Accent4.SrgbClr.Val,
8: clrScheme.Accent5.SrgbClr.Val,
9: clrScheme.Accent6.SrgbClr.Val,
0: clrScheme.Lt1.colorChoice(),
1: clrScheme.Dk1.colorChoice(),
2: clrScheme.Lt2.colorChoice(),
3: clrScheme.Dk2.colorChoice(),
4: clrScheme.Accent1.colorChoice(),
5: clrScheme.Accent2.colorChoice(),
6: clrScheme.Accent3.colorChoice(),
7: clrScheme.Accent4.colorChoice(),
8: clrScheme.Accent5.colorChoice(),
9: clrScheme.Accent6.colorChoice(),
}[*themeColor]; ok && val != nil {
return *val
}
Expand Down
2 changes: 2 additions & 0 deletions styles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,8 @@ func TestGetThemeColor(t *testing.T) {
assert.Equal(t, "FFFFFF", f.getThemeColor(&xlsxColor{RGB: "FFFFFF"}))
assert.Equal(t, "FF8080", f.getThemeColor(&xlsxColor{Indexed: 2, 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) {
Expand Down
2 changes: 1 addition & 1 deletion xmlTheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ type xlsxEffectStyleLst struct {
EffectStyleLst string `xml:",innerxml"`
}

// xlsxBgFillStyleLst element defines a list of background fills that are
// xlsxBgFillStyleLst element defines a list of background fills that are
// used within a theme. The background fills consist of three fills, arranged
// in order from subtle to moderate to intense.
type xlsxBgFillStyleLst struct {
Expand Down

0 comments on commit 30c4cd7

Please sign in to comment.