gitea源码

color_test.go 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package util
  4. import (
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func Test_HexToRBGColor(t *testing.T) {
  9. cases := []struct {
  10. colorString string
  11. expectedR float64
  12. expectedG float64
  13. expectedB float64
  14. }{
  15. {"2b8685", 43, 134, 133},
  16. {"1e1", 17, 238, 17},
  17. {"#1e1", 17, 238, 17},
  18. {"1e16", 17, 238, 17},
  19. {"3bb6b3", 59, 182, 179},
  20. {"#3bb6b399", 59, 182, 179},
  21. {"#0", 0, 0, 0},
  22. {"#00000", 0, 0, 0},
  23. {"#1234567", 0, 0, 0},
  24. }
  25. for n, c := range cases {
  26. r, g, b := HexToRBGColor(c.colorString)
  27. assert.InDelta(t, c.expectedR, r, 0, "case %d: error R should match: expected %f, but get %f", n, c.expectedR, r)
  28. assert.InDelta(t, c.expectedG, g, 0, "case %d: error G should match: expected %f, but get %f", n, c.expectedG, g)
  29. assert.InDelta(t, c.expectedB, b, 0, "case %d: error B should match: expected %f, but get %f", n, c.expectedB, b)
  30. }
  31. }
  32. func Test_UseLightText(t *testing.T) {
  33. cases := []struct {
  34. color string
  35. expected string
  36. }{
  37. {"#d73a4a", "#fff"},
  38. {"#0075ca", "#fff"},
  39. {"#cfd3d7", "#000"},
  40. {"#a2eeef", "#000"},
  41. {"#7057ff", "#fff"},
  42. {"#008672", "#fff"},
  43. {"#e4e669", "#000"},
  44. {"#d876e3", "#000"},
  45. {"#ffffff", "#000"},
  46. {"#2b8684", "#fff"},
  47. {"#2b8786", "#fff"},
  48. {"#2c8786", "#000"},
  49. {"#3bb6b3", "#000"},
  50. {"#7c7268", "#fff"},
  51. {"#7e716c", "#fff"},
  52. {"#81706d", "#fff"},
  53. {"#807070", "#fff"},
  54. {"#84b6eb", "#000"},
  55. }
  56. for n, c := range cases {
  57. assert.Equal(t, c.expected, ContrastColor(c.color), "case %d: error should match", n)
  58. }
  59. }