From 30c4cd70e04ad511dd3fd36f083168dffd06feac Mon Sep 17 00:00:00 2001 From: xuri Date: Tue, 23 Jul 2024 21:48:17 +0800 Subject: [PATCH] This close #1963, prevent the GetStyle function panic when theme without sysClr --- styles.go | 31 +++++++++++++++++++++---------- styles_test.go | 2 ++ xmlTheme.go | 2 +- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/styles.go b/styles.go index 7aee0ff..64c0bc0 100644 --- a/styles.go +++ b/styles.go @@ -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 } diff --git a/styles_test.go b/styles_test.go index 1c309bc..1b5d3a2 100644 --- a/styles_test.go +++ b/styles_test.go @@ -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) { diff --git a/xmlTheme.go b/xmlTheme.go index ec0c2bd..6bbabf6 100644 --- a/xmlTheme.go +++ b/xmlTheme.go @@ -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 {