Skip to content

Latest commit

 

History

History
32 lines (23 loc) · 1.02 KB

convert-a-hex-string-to-rgb.md

File metadata and controls

32 lines (23 loc) · 1.02 KB

Convert A Hex String To RGB 5 Kyu

LINK TO THE KATA - PARSING STRINGS ALGORITHMS

Description

When working with color values it can sometimes be useful to extract the individual red, green, and blue (RGB) component values for a color. Implement a function that meets these requirements:

  • Accepts a case-insensitive hexadecimal color string as its parameter (ex. "#FF9933" or "#ff9933")
  • Returns a Map<String, int> with the structure {r: 255, g: 153, b: 51} where r, g, and b range from 0 through 255

Note: your implementation does not need to support the shorthand form of hexadecimal notation (ie "#FFF")

Example

"#FF9933" --> {r: 255, g: 153, b: 51}

Solution

const hexStringToRGB = hex => ({
  r: parseInt(hex.slice(1, 3), 16),
  g: parseInt(hex.slice(3, 5), 16),
  b: parseInt(hex.slice(5, 7), 16),
})