theme.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package xlsx
  2. import (
  3. "fmt"
  4. "strconv"
  5. )
  6. type theme struct {
  7. colors []string
  8. }
  9. func newTheme(themeXml xlsxTheme) *theme {
  10. clrMap := map[string]string{}
  11. clrSchemes := themeXml.ThemeElements.ClrScheme.Children
  12. for _, scheme := range clrSchemes {
  13. var rgbColor string
  14. if scheme.SysClr != nil {
  15. rgbColor = scheme.SysClr.LastClr
  16. } else {
  17. rgbColor = scheme.SrgbClr.Val
  18. }
  19. clrMap[scheme.XMLName.Local] = rgbColor
  20. }
  21. colors := []string{clrMap["lt1"], clrMap["dk1"], clrMap["lt2"], clrMap["dk2"], clrMap["accent1"],
  22. clrMap["accent2"], clrMap["accent3"], clrMap["accent4"], clrMap["accent5"],
  23. clrMap["accent6"], clrMap["hlink"], clrMap["folHlink"]}
  24. return &theme{colors}
  25. }
  26. func (t *theme) themeColor(index int64, tint float64) string {
  27. baseColor := t.colors[index]
  28. if tint == 0 {
  29. return "FF" + baseColor
  30. } else {
  31. r, _ := strconv.ParseInt(baseColor[0:2], 16, 64)
  32. g, _ := strconv.ParseInt(baseColor[2:4], 16, 64)
  33. b, _ := strconv.ParseInt(baseColor[4:6], 16, 64)
  34. h, s, l := RGBToHSL(uint8(r), uint8(g), uint8(b))
  35. if tint < 0 {
  36. l *= (1 + tint)
  37. } else {
  38. l = l*(1-tint) + (1 - (1 - tint))
  39. }
  40. br, bg, bb := HSLToRGB(h, s, l)
  41. return fmt.Sprintf("FF%02X%02X%02X", br, bg, bb)
  42. }
  43. }