色值计算

  1. func ThemeColor(baseColor string, tint float64) string

通过给定的 RGB 格式色值与色调参数,计算出最终颜色。例如,获取名为 Sheet1 的工作表 A1 单元格的背景颜色:

  1. package main
  2. import (
  3. "strings"
  4. "github.com/360EntSecGroup-Skylar/excelize"
  5. )
  6. func main() {
  7. f, err := excelize.OpenFile("Book1.xlsx")
  8. if err != nil {
  9. println(err.Error())
  10. return
  11. }
  12. println(getCellBgColor(f, "Sheet1", "A1"))
  13. }
  14. func getCellBgColor(f *excelize.File, sheet, axix string) string {
  15. styleID, err := f.GetCellStyle(sheet, axix)
  16. if err != nil {
  17. return err.Error()
  18. }
  19. fillID := f.Styles.CellXfs.Xf[styleID].FillID
  20. fgColor := f.Styles.Fills.Fill[fillID].PatternFill.FgColor
  21. if fgColor.Theme != nil {
  22. children := f.Theme.ThemeElements.ClrScheme.Children
  23. if *fgColor.Theme < 4 {
  24. dklt := map[int]string{
  25. 0: children[1].SysClr.LastClr,
  26. 1: children[0].SysClr.LastClr,
  27. 2: children[3].SrgbClr.Val,
  28. 3: children[2].SrgbClr.Val,
  29. }
  30. return strings.TrimPrefix(excelize.ThemeColor(dklt[*fgColor.Theme], fgColor.Tint), "FF")
  31. }
  32. srgbClr := children[*fgColor.Theme].SrgbClr.Val
  33. return strings.TrimPrefix(excelize.ThemeColor(srgbClr, fgColor.Tint), "FF")
  34. }
  35. return strings.TrimPrefix(fgColor.RGB, "FF")
  36. }