Skip to content

Commit 99e91e1

Browse files
xxxwang1983wangjingwei
andauthored
This closes #1794, add new GetBaseColor function (#1798)
Co-authored-by: wangjingwei <[email protected]>
1 parent 9a68553 commit 99e91e1

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

styles.go

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,14 +1369,11 @@ var (
13691369
}
13701370
)
13711371

1372-
// getThemeColor provides a function to convert theme color or index color to
1373-
// RGB color.
1374-
func (f *File) getThemeColor(clr *xlsxColor) string {
1375-
var RGB string
1376-
if clr == nil || f.Theme == nil {
1377-
return RGB
1378-
}
1379-
if clrScheme := f.Theme.ThemeElements.ClrScheme; clr.Theme != nil {
1372+
// GetBaseColor returns the preferred hex color code by giving hex color code,
1373+
// indexed color, and theme color.
1374+
func (f *File) GetBaseColor(hexColor string, indexedColor int, themeColor *int) string {
1375+
if f.Theme != nil && themeColor != nil {
1376+
clrScheme := f.Theme.ThemeElements.ClrScheme
13801377
if val, ok := map[int]*string{
13811378
0: &clrScheme.Lt1.SysClr.LastClr,
13821379
1: &clrScheme.Dk1.SysClr.LastClr,
@@ -1388,21 +1385,35 @@ func (f *File) getThemeColor(clr *xlsxColor) string {
13881385
7: clrScheme.Accent4.SrgbClr.Val,
13891386
8: clrScheme.Accent5.SrgbClr.Val,
13901387
9: clrScheme.Accent6.SrgbClr.Val,
1391-
}[*clr.Theme]; ok && val != nil {
1392-
return strings.TrimPrefix(ThemeColor(*val, clr.Tint), "FF")
1388+
}[*themeColor]; ok && val != nil {
1389+
return *val
13931390
}
13941391
}
1395-
if len(clr.RGB) == 6 {
1396-
return clr.RGB
1392+
if len(hexColor) == 6 {
1393+
return hexColor
1394+
}
1395+
if len(hexColor) == 8 {
1396+
return strings.TrimPrefix(hexColor, "FF")
1397+
}
1398+
if f.Styles != nil && f.Styles.Colors != nil && f.Styles.Colors.IndexedColors != nil &&
1399+
indexedColor < len(f.Styles.Colors.IndexedColors.RgbColor) {
1400+
return strings.TrimPrefix(f.Styles.Colors.IndexedColors.RgbColor[indexedColor].RGB, "FF")
13971401
}
1398-
if len(clr.RGB) == 8 {
1399-
return strings.TrimPrefix(clr.RGB, "FF")
1402+
if indexedColor < len(IndexedColorMapping) {
1403+
return IndexedColorMapping[indexedColor]
14001404
}
1401-
if f.Styles.Colors != nil && f.Styles.Colors.IndexedColors != nil && clr.Indexed < len(f.Styles.Colors.IndexedColors.RgbColor) {
1402-
return strings.TrimPrefix(ThemeColor(strings.TrimPrefix(f.Styles.Colors.IndexedColors.RgbColor[clr.Indexed].RGB, "FF"), clr.Tint), "FF")
1405+
return hexColor
1406+
}
1407+
1408+
// getThemeColor provides a function to convert theme color or index color to
1409+
// RGB color.
1410+
func (f *File) getThemeColor(clr *xlsxColor) string {
1411+
var RGB string
1412+
if clr == nil || f.Theme == nil {
1413+
return RGB
14031414
}
1404-
if clr.Indexed < len(IndexedColorMapping) {
1405-
return strings.TrimPrefix(ThemeColor(IndexedColorMapping[clr.Indexed], clr.Tint), "FF")
1415+
if RGB = f.GetBaseColor(clr.RGB, clr.Indexed, clr.Theme); RGB != "" {
1416+
RGB = strings.TrimPrefix(ThemeColor(RGB, clr.Tint), "FF")
14061417
}
14071418
return RGB
14081419
}

0 commit comments

Comments
 (0)