Skip to content

Instantly share code, notes, and snippets.

@alexrqs
Created August 31, 2019 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexrqs/23e053f53c956bc11fc329e539879e5c to your computer and use it in GitHub Desktop.
Save alexrqs/23e053f53c956bc11fc329e539879e5c to your computer and use it in GitHub Desktop.
RGB function with params per color to string
const normalColor = number => number > 255 ? 255 : number < 0 ? 0 : number
const toHEX = color => Number(normalColor(color)).toString(16).toUpperCase()
const normalHEX = hex => ((hexi = toHEX(hex)) => hexi.toString().length < 2 ? '0' + hexi : hexi)()
const rgb = (r,g,b) => normalHEX(r) + normalHEX(g) + normalHEX(b)
Test.assertEquals(rgb(0, 0, 0), '000000')
Test.assertEquals(rgb(0, 0, -20), '000000')
Test.assertEquals(rgb(300,255,255), 'FFFFFF')
Test.assertEquals(rgb(173,255,47), 'ADFF2F')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment