Hex triplets such as #ffffff
(white) or #3764ef
(blueish) are often used to denote RGB colors. They consist of #
followed by six hexadecimal digits (0-f), or sometimes three digits where the real color is obtained by doubling each digit. For example, #fff
is #ffffff
and #1a8
is #11aa88
.
Sadly, that three digit shorthand was the golfiest the internet had to offer, until now.
Write a program or function that takes in a string of 1 to 7 characters:
- The first character will always be
#
. - The other characters will always be hexadecimal digits:
0123456789abcdef
.
The input is a shorthand form of a hex triplet (or the full form if 7 characters are given). You need to output a full hex triplet that expands the input shorthand based on these patterns:
Input -> Output
# -> #000000 (black)
#U -> #UUUUUU
#UV -> #UVUVUV
#UVW -> #UUVVWW (usual 3-digit shorthand)
#UVWX -> #UXVXWX
#UVWXY -> #UVWXYY
#UVWXYZ -> #UVWXYZ (not shorthand)
Each of U
, V
, W
, X
, Y
, and Z
may be any hexadecimal digit. The output is always 7 characters.
For example:
Input -> Output
# -> #000000
#0 -> #000000
#4 -> #444444
#f -> #ffffff
#a1 -> #a1a1a1
#0f -> #0f0f0f
#99 -> #999999
#1a8 -> #11aa88
#223 -> #222233
#fff -> #ffffff
#1230 -> #102030
#d767 -> #d77767
#bbb5 -> #b5b5b5
#aabbc -> #aabbcc
#00000 -> #000000
#3764e -> #3764ee
#3764ef -> #3764ef
#123456 -> #123456
#f8f8f8 -> #f8f8f8
Notes
The input will always start with
#
and so must the output.You may assume all input letters are lowercase (
abcdef
) or uppercase (ABCDEF
) as you prefer.Letters in the output may be in either case as you prefer. You can even mix cases.
Alpha/transparency is not dealt with here (though there are hex versions of RGBA colors).
The shortest code in bytes wins.
#UVWXY -> #UVWXYY
entry because it allows for a single-value representation for the Blue channel, but there's no similar expression for Red and Green (e.g. if I wanted#889071
I can't abbreviate, but#907188
can be...as#90718
) the others all work great. \$\endgroup\$#UVWX -> #UXVXWX
. It's such an inconsistent and arbitrary behaviour that it's hard to believe there isn't a couple of browser that actually currently implement it. \$\endgroup\$#RGBA
and#RRGGBBAA
, so #1234 should be read asrgba(17, 34, 51, 0.25)
\$\endgroup\$