Skip to main content
fix spelling
Source Link
wrymug
  • 810
  • 4
  • 3

Find the closest hex colorcolour shorthand

In CSS, colorscolours can be specified by a "hex triplet" - a three byte (six digit) hexadecimal number where each byte represents the red, green, or blue components of the colorcolour. For instance, #FF0000 is completely red, and is equivalent to rgb(255, 0, 0).

ColorsColours can also be represented by the shorthand notation which uses three hexadecimal digits. The shorthand expands to the six digit form by duplicating each digit. For instance, #F00#ABC becomes #FF0000#AABBCC.

Since there are fewer digits in the hex shorthand, fewer colorscolours can be represented.

The challenge

Write a program or function that takes a six digit hexadecimal color code and outputs the closest three-digit color code, where closeness is measured by adding together the difference between each component of the full color code and the corresponding component of the shorthand color code.

Here's an example:

  • Input hex code: #28a086
  • red component
    • 0x28 = 40 (decimal)
    • 0x22 = 30
    • 0x33 = 51
    • 0x22 is closer, so the first digit of the shortened color code is 2
  • green component
    • 0xa0 = 160
    • 0x99 = 153
    • 0xaa = 170
    • 0x99 is closer, so the second digit is 9
  • blue component
    • 0x86 = 134
    • 0x77 = 119
    • 0x88 = 136
    • 0x88 is closer, so the third digit is 8
  • The shortened color code is #298

Your program or function must accept as input a six digit hexadecimal color code prepended with # and output a three digit color code prepended with #.

Examples

  • #FF0000 → #F00
  • #00FF00 → #0F0
  • #D913C4 → #D1C
  • #C0DD39 → #BD3
  • #28A086 → #298
  • #C0CF6F → #BC7

Find the closest hex color shorthand

In CSS, colors can be specified by a "hex triplet" - a three byte (six digit) hexadecimal number where each byte represents the red, green, or blue components of the color. For instance, #FF0000 is completely red, and is equivalent to rgb(255, 0, 0).

Colors can also be represented by the shorthand notation which uses three hexadecimal digits. The shorthand expands to the six digit form by duplicating each digit. For instance, #F00 becomes #FF0000.

Since there are fewer digits in the hex shorthand, fewer colors can be represented.

The challenge

Write a program or function that takes a six digit hexadecimal color code and outputs the closest three-digit color code, where closeness is measured by adding together the difference between each component of the full color code and the corresponding component of the shorthand color code.

Your program or function must accept as input a six digit hexadecimal color code prepended with # and output a three digit color code prepended with #.

Examples

  • #FF0000 → #F00
  • #00FF00 → #0F0
  • #D913C4 → #D1C
  • #C0DD39 → #BD3

Find the closest hex colour shorthand

In CSS, colours can be specified by a "hex triplet" - a three byte (six digit) hexadecimal number where each byte represents the red, green, or blue components of the colour. For instance, #FF0000 is completely red, and is equivalent to rgb(255, 0, 0).

Colours can also be represented by the shorthand notation which uses three hexadecimal digits. The shorthand expands to the six digit form by duplicating each digit. For instance, #ABC becomes #AABBCC.

Since there are fewer digits in the hex shorthand, fewer colours can be represented.

The challenge

Write a program or function that takes a six digit hexadecimal color code and outputs the closest three-digit color code, where closeness is measured by adding together the difference between each component of the full color code and the corresponding component of the shorthand color code.

Here's an example:

  • Input hex code: #28a086
  • red component
    • 0x28 = 40 (decimal)
    • 0x22 = 30
    • 0x33 = 51
    • 0x22 is closer, so the first digit of the shortened color code is 2
  • green component
    • 0xa0 = 160
    • 0x99 = 153
    • 0xaa = 170
    • 0x99 is closer, so the second digit is 9
  • blue component
    • 0x86 = 134
    • 0x77 = 119
    • 0x88 = 136
    • 0x88 is closer, so the third digit is 8
  • The shortened color code is #298

Your program or function must accept as input a six digit hexadecimal color code prepended with # and output a three digit color code prepended with #.

Examples

  • #FF0000 → #F00
  • #00FF00 → #0F0
  • #D913C4 → #D1C
  • #C0DD39 → #BD3
  • #28A086 → #298
  • #C0CF6F → #BC7
Source Link
wrymug
  • 810
  • 4
  • 3

Find the closest hex color shorthand

In CSS, colors can be specified by a "hex triplet" - a three byte (six digit) hexadecimal number where each byte represents the red, green, or blue components of the color. For instance, #FF0000 is completely red, and is equivalent to rgb(255, 0, 0).

Colors can also be represented by the shorthand notation which uses three hexadecimal digits. The shorthand expands to the six digit form by duplicating each digit. For instance, #F00 becomes #FF0000.

Since there are fewer digits in the hex shorthand, fewer colors can be represented.

The challenge

Write a program or function that takes a six digit hexadecimal color code and outputs the closest three-digit color code, where closeness is measured by adding together the difference between each component of the full color code and the corresponding component of the shorthand color code.

Your program or function must accept as input a six digit hexadecimal color code prepended with # and output a three digit color code prepended with #.

Examples

  • #FF0000 → #F00
  • #00FF00 → #0F0
  • #D913C4 → #D1C
  • #C0DD39 → #BD3