46
6
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.
11
"Sadly, that three digit shorthand was the golfiest the internet had to offer, until now." - Uh, not exactly. HTML, 0 bytes - it works out of the box
– Bergi – 2017-05-30T08:47:54.03011The reverse of this would be a cool challenge too – Beta Decay – 2017-05-30T09:20:12.377
9I am unsatisfied with the
#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. – Draco18s no longer trusts SE – 2017-05-30T19:07:33.7203@Draco18s I love it. That one and the
#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. – xDaizu – 2017-05-31T07:37:18.1631@xDaizu CSS specification has
#RGBA
and#RRGGBBAA
, so #1234 should be read asrgba(17, 34, 51, 0.25)
– tsh – 2017-05-31T07:40:18.837@tsh Boy oh boy, conflicting interpretations? In HTML/CSS? Loving it more and more! – xDaizu – 2017-05-31T08:01:30.220
@xDaizu
it's hard to believe there isn't a couple of browsers that actually currently implement it
Ha! Point to you sir. (I do like#UVWX -> #UXVXWX
though, that's clever). – Draco18s no longer trusts SE – 2017-05-31T13:42:35.623@Draco18s The 5-digit one is weird but I'm not sure there's a nice way to make it symmetric, except for awkward stuff like
#U(X+Y)V(X+Y)W(X+Y)
. – Calvin's Hobbies – 2017-05-31T16:40:41.767Oh agreed, I was just voicing my displeasure. More at the fact that there isn't a good solution rather than your particular one. – Draco18s no longer trusts SE – 2017-05-31T16:44:06.573
#UVWXYY makes sense because blue is the color that looks the darkest, and has the least effect on the output. It's also pretty common to store colors in 16 bits with 6 bits for green and 5 each for red and blue. – 12Me21 – 2019-06-26T19:44:11.190