Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.

rgb2hex.test.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. const rgb2hex = require('../index')
  2. const typeofErrorMessage = 'color has to be type of `string`'
  3. const invalidErrorMessage = (input) => 'given color (' + input + ') isn\'t a valid rgb or rgba color'
  4. describe('rgb2hex should', () => {
  5. describe('throw an error if input is not typeof string', () => {
  6. it('[Object] {color: \'something\'}', () => {
  7. const input = {color: 'something'}
  8. expect(() => rgb2hex(input)).toThrow(typeofErrorMessage)
  9. })
  10. it('[Function] function(){}', () => {
  11. const input = function(){}
  12. expect(() => rgb2hex(input)).toThrow(typeofErrorMessage)
  13. })
  14. it('[Number] 231', () => {
  15. const input = 231
  16. expect(() => rgb2hex(input)).toThrow(typeofErrorMessage)
  17. })
  18. })
  19. describe('throw an error if input is invalid', () => {
  20. it('notacolor', () => {
  21. const input = 'notacolor'
  22. expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
  23. })
  24. it('rgba(100, 100)', () => {
  25. const input = 'rgb(100, 100)'
  26. expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
  27. })
  28. it('rgba(100, 10a0, 200, 300)', () => {
  29. const input = 'rgba(100, 10a0, 200, 300)'
  30. expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
  31. })
  32. it('rgba(23, 54, 4, -.33)', () => {
  33. const input = 'rgba(23, 54, 4, -.33)'
  34. expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
  35. })
  36. })
  37. it('return input if it is already a hex color', () => {
  38. const input = '#ffffff'
  39. const parsedValue = rgb2hex(input)
  40. expect(parsedValue).toHaveProperty('hex')
  41. expect(parsedValue).toHaveProperty('alpha')
  42. expect(typeof parsedValue.hex).toEqual('string')
  43. expect(parsedValue.hex).toEqual('#ffffff')
  44. expect(typeof parsedValue.alpha).toEqual('number')
  45. expect(parsedValue.alpha).toEqual(1)
  46. })
  47. describe('parse input properly', () => {
  48. it('converting rgb(210,43,255)', () => {
  49. const input = 'rgb(210,43,255)'
  50. const parsedValue = rgb2hex(input)
  51. expect(parsedValue).toHaveProperty('hex')
  52. expect(parsedValue).toHaveProperty('alpha')
  53. expect(typeof parsedValue.hex).toEqual('string')
  54. expect(parsedValue.hex).toEqual('#d22bff')
  55. expect(typeof parsedValue.alpha).toEqual('number')
  56. expect(parsedValue.alpha).toEqual(1)
  57. })
  58. it('converting rgba(12,173,22,.67313)', () => {
  59. const input = 'rgba(12,173,22,.67313)'
  60. const parsedValue = rgb2hex(input)
  61. expect(parsedValue).toHaveProperty('hex')
  62. expect(parsedValue).toHaveProperty('alpha')
  63. expect(typeof parsedValue.hex).toEqual('string')
  64. expect(parsedValue.hex).toEqual('#0cad16')
  65. expect(typeof parsedValue.alpha).toEqual('number')
  66. expect(parsedValue.alpha).toEqual(0.67)
  67. })
  68. it('by limiting alpha value to 1', () => {
  69. const input = 'rgba(236,68,44,1)'
  70. expect(rgb2hex(input).alpha).not.toBeGreaterThan(1)
  71. })
  72. it('by not accepting to big values', () => {
  73. let input = 'rgba(1123, 54, 4, 0.33)'
  74. expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
  75. input = 'rgba(113, 1154, 4, 0.33)'
  76. expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
  77. input = 'rgba(113, 154, 1114, 0.33)'
  78. expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
  79. input = 'rgba(113, 54, 4, 2.33)'
  80. expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
  81. input = 'rgbaaaaaa(113, 54, 4, .33)'
  82. expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
  83. input = 'rgba(12,173,22,1.67)'
  84. expect(() => rgb2hex(input)).toThrow(invalidErrorMessage(input))
  85. })
  86. it('transparent color', () => {
  87. const input = 'rgba(0, 0, 0, 0)'
  88. expect(rgb2hex(input).alpha).toBe(0)
  89. expect(rgb2hex(input).hex).toBe('#000000')
  90. })
  91. it('double digit alpha values', () => {
  92. const input = 'rgba(0, 0, 0, 1.00)'
  93. expect(rgb2hex(input).alpha).toBe(1)
  94. expect(rgb2hex(input).hex).toBe('#000000')
  95. })
  96. })
  97. describe('not care about', () => {
  98. it('rgb or rgba prefix', () => {
  99. const rgb = 'rgb(0, 0, 0)'
  100. const rgba = 'rgba(0, 0, 0)'
  101. expect(rgb2hex(rgb).hex).toEqual(rgb2hex(rgba).hex)
  102. })
  103. it('spaces between color numbers', () => {
  104. const rgbWithSpaces = 'rgb(0, 0, 0)'
  105. const rgbaWithoutSpaces = 'rgba(0,0,0)'
  106. expect(rgb2hex(rgbWithSpaces).hex).toEqual(rgb2hex(rgbaWithoutSpaces).hex)
  107. })
  108. it('if alpha value starts with `.` or with `0`', () => {
  109. const rgbaAlphaStartsWithDot = 'rgba(213,12,4,.45)'
  110. const rgbaAlphaStartsWithZero = 'rgba(213,12,4,0.45)'
  111. expect(rgb2hex(rgbaAlphaStartsWithDot).alpha).toEqual(rgb2hex(rgbaAlphaStartsWithZero).alpha)
  112. })
  113. it('optional terminating semicolon', () => {
  114. const rgbWithTerminatingSemicolon = 'rgb(0,0,0);'
  115. const rgbWithoutTerminatingSemicolon = 'rgb(0,0,0)'
  116. expect(rgb2hex(rgbWithTerminatingSemicolon).hex).toEqual(rgb2hex(rgbWithoutTerminatingSemicolon).hex)
  117. })
  118. it('stuff that is appended', () => {
  119. expect(rgb2hex('rgb(0,0,0)0px0px8px').hex).toEqual(rgb2hex('rgb(0,0,0)').hex)
  120. expect(rgb2hex('rgb(0,0,0)solid2px').hex).toEqual(rgb2hex('rgb(0,0,0)').hex)
  121. })
  122. it('stuff that is prepended', () => {
  123. expect(rgb2hex('0px0px8pxrgb(0,0,0)').hex).toEqual(rgb2hex('rgb(0,0,0)').hex)
  124. expect(rgb2hex('solid2pxrgb(0,0,0)').hex).toEqual(rgb2hex('rgb(0,0,0)').hex)
  125. })
  126. it('stuff that is prepended and appended', () => {
  127. const url = 'https://foo.bar.com/123.abc.456'
  128. const values = `url("${url}")no-repeatscroll0%0%/100%padding-boxborder-box`
  129. expect(rgb2hex(`${values}rgb(226,230,233)${values}`).hex)
  130. expect(rgb2hex(`${values}rgba(226,230,233,0.4)${values}`).hex)
  131. })
  132. })
  133. })