Skip to the code
cf.
+10 XP

#668 Unpack a Packed Color

easy

A canvas tool stores each pixel as one 32-bit number laid out as 0xRRGGBBAA: the highest 8 bits are red, then green, then blue, and the lowest 8 bits are alpha. The number is given as an unsigned value from 0 to 4294967295.

Write unpackColor(color) that returns [red, green, blue, alpha], each from 0 to 255.

Example 1

Input: color = 4278190335
Output: [255,0,0,255]

4278190335 is 0xFF0000FF: red 255, green 0, blue 0, alpha 255.

Example 2

Input: color = 0
Output: [0,0,0,0]

All bits are 0: transparent black.

Constraints

  • 0 <= color <= 4294967295
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).