You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

color_test.go 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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.Equal(t, c.expectedR, r, "case %d: error R should match: expected %f, but get %f", n, c.expectedR, r)
  28. assert.Equal(t, c.expectedG, g, "case %d: error G should match: expected %f, but get %f", n, c.expectedG, g)
  29. assert.Equal(t, c.expectedB, b, "case %d: error B should match: expected %f, but get %f", n, c.expectedB, b)
  30. }
  31. }
  32. func Test_UseLightTextOnBackground(t *testing.T) {
  33. cases := []struct {
  34. r float64
  35. g float64
  36. b float64
  37. expected bool
  38. }{
  39. {215, 58, 74, true},
  40. {0, 117, 202, true},
  41. {207, 211, 215, false},
  42. {162, 238, 239, false},
  43. {112, 87, 255, true},
  44. {0, 134, 114, true},
  45. {228, 230, 105, false},
  46. {216, 118, 227, true},
  47. {255, 255, 255, false},
  48. {43, 134, 133, true},
  49. {43, 135, 134, true},
  50. {44, 135, 134, true},
  51. {59, 182, 179, true},
  52. {124, 114, 104, true},
  53. {126, 113, 108, true},
  54. {129, 112, 109, true},
  55. {128, 112, 112, true},
  56. }
  57. for n, c := range cases {
  58. result := UseLightTextOnBackground(c.r, c.g, c.b)
  59. assert.Equal(t, c.expected, result, "case %d: error should match", n)
  60. }
  61. }