rgb2hex.go 599 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package excel
  2. import "strconv"
  3. // RGB与HEX颜色互转
  4. type RGB struct {
  5. Red,
  6. Green,
  7. Blue int64
  8. }
  9. type HEX struct {
  10. Str string
  11. }
  12. func t2x(t int64) string {
  13. result := strconv.FormatInt(t, 16)
  14. if len(result) == 1 {
  15. result = "0" + result
  16. }
  17. return result
  18. }
  19. func (color RGB) Rgb2Hex() HEX {
  20. r := t2x(color.Red)
  21. g := t2x(color.Green)
  22. b := t2x(color.Blue)
  23. return HEX{r + g + b}
  24. }
  25. func (color HEX) Hex2Rgb() RGB {
  26. r, _ := strconv.ParseInt(color.Str[:2], 16, 10)
  27. g, _ := strconv.ParseInt(color.Str[2:4], 16, 18)
  28. b, _ := strconv.ParseInt(color.Str[4:], 16, 10)
  29. return RGB{r, g, b}
  30. }