HEX to RGB

Medium

Prompt

Write a JavaScript function that takes a string representing a color in hexadecimal format and returns an object representing the equivalent color in RGB (Red, Green, Blue) format.

  • The input will be a string starting with # followed by either 3 or 6 hexadecimal characters (e.g. #fff or #0080ff).
  • For 3-character shorthand, each character should be doubled (e.g. #abc becomes #aabbcc).
  • The resulting object should have keys r, g, and b, each holding a decimal number between 0 and 255.
  • Do not use regular expressions or external libraries.