色值计算

  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, cell string) string {
  19. styleID, err := f.GetCellStyle(sheet, cell)
  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 != nil && f.Theme != nil {
  26. if clrScheme := f.Theme.ThemeElements.ClrScheme; fgColor.Theme != nil {
  27. if val, ok := map[int]*string{
  28. 0: &clrScheme.Lt1.SysClr.LastClr,
  29. 1: &clrScheme.Dk1.SysClr.LastClr,
  30. 2: clrScheme.Lt2.SrgbClr.Val,
  31. 3: clrScheme.Dk2.SrgbClr.Val,
  32. 4: clrScheme.Accent1.SrgbClr.Val,
  33. 5: clrScheme.Accent2.SrgbClr.Val,
  34. 6: clrScheme.Accent3.SrgbClr.Val,
  35. 7: clrScheme.Accent4.SrgbClr.Val,
  36. 8: clrScheme.Accent5.SrgbClr.Val,
  37. 9: clrScheme.Accent6.SrgbClr.Val,
  38. }[*fgColor.Theme]; ok && val != nil {
  39. return strings.TrimPrefix(excelize.ThemeColor(*val, fgColor.Tint), "FF")
  40. }
  41. }
  42. return strings.TrimPrefix(fgColor.RGB, "FF")
  43. }
  44. return "FFFFFF"
  45. }