[05.17.22] Coding test

Juyeon.it·2022년 5월 7일
0

Coding test

목록 보기
26/32

Convert A Hex String To RGB

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}

My answer

function hexStringToRGB(hexString) {
  let array = [hexString.slice(1,3), hexString.slice(3,5), hexString.slice(5,7)];
  let result = {}
  
  result.r = parseInt(array[0], 16);
  result.g = parseInt(array[1], 16);
  result.b = parseInt(array[2], 16);
  
  return result;
}

Other solutions

const hexStringToRGB = str => {
  const [r, g, b] = str.match(/\w{2}/g).map(x => parseInt(x, 16));
  return { r, g, b };
}

0개의 댓글

관련 채용 정보