Skip to main content
added 1 character in body
Source Link
Calvin's Hobbies
  • 90.8k
  • 46
  • 348
  • 565

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 (abdefabcdef) 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.

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 (abdef) 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.

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.

Tweeted twitter.com/StackCodeGolf/status/869400638928670721
Source Link
Calvin's Hobbies
  • 90.8k
  • 46
  • 348
  • 565

Better Hex Color Codes for Your Internet

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 (abdef) 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.