Skip to the code
cf.
+10 XP

#653 Hex Color to RGB

easy

A color picker accepts CSS hex colors and shows their red, green, and blue values. A hex color is # followed by 6 hex digits (#rrggbb) or 3 hex digits (#rgb, where each digit is doubled, so #abc means #aabbcc). Letters may be upper or lower case.

Write hexToRgb(hex) that returns [red, green, blue], each from 0 to 255.

Example 1

Input: hex = "#1a2b3c"
Output: [26,43,60]

1a = 1 × 16 + 10 = 26, 2b = 43, and 3c = 60.

Example 2

Input: hex = "#fff"
Output: [255,255,255]

#fff means #ffffff, and ff = 15 × 16 + 15 = 255.

Constraints

  • hex is a valid 4- or 7-character hex color.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).