色值计算

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

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

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