This page is still under construction.

Parts of this page are still being built. What you see may change.

Closest Web Color Name

Interview

Time limit2sMemory limit256 MB

Summary
The program maps each RGB triple to the closest of 16 named HTML colors by Euclidean distance, breaking ties by table order.
Level

Easy2 of 10

Topics
Brute force, Math
Solved
No attempts yet

Problem

Web colors are the colors used to display web pages. A single color is given either as an RGB triple or as the English name of that color. A color is set by the intensity of its red, green and blue components, and each component takes eight bits, so one web color needs 24 bits and there are 16,777,216 web colors in total. The HTML 4 specification names only the 16 colors in the table below.

Mapping an arbitrary color to one of the named HTML colors is a common task. This problem asks for that mapping in RGB space.

The color closest to a given color is the one at the smallest Euclidean distance. If the color to map is (r,g,b)(r, g, b) and the 16 HTML colors are (R1,G1,B1)(R_1, G_1, B_1) through (R16,G16,B16)(R_{16}, G_{16}, B_{16}), the closest color minimizes

d=(Ri−r)2+(Gi−g)2+(Bi−b)2d = \sqrt{(R_i - r)^2 + (G_i - g)^2 + (B_i - b)^2}

where ii is an integer from 1 to 16.

#NameRedGreenBlue
1White255255255
2Silver192192192
3Gray128128128
4Black000
5Red25500
6Maroon12800
7Yellow2552550
8Olive1281280
9Lime02550
10Green01280
11Aqua0255255
12Teal0128128
13Blue00255
14Navy00128
15Fuchsia2550255
16Purple1280128

Input

The input holds several test cases. Each test case is one line with three integers rr, gg, bb (0≤r,g,b≤2550 \le r, g, b \le 255), the red, green and blue intensities of a color.

The last line of the input is -1 -1 -1 and is not processed.

Output

For each test case, print on its own line the name of the HTML color closest to the given color, spelled as in the table. If two or more colors are equally close, print the name of the one with the smaller number in the table.

Examples3

  1. Example 1

    Input
    120 120 10
    111 112 113
    5 135 8
    -1 -1 -1
    
    Expected output
    Olive
    Gray
    Green
    
  2. Example 2

    Input
    255 255 255
    192 192 192
    128 128 128
    0 0 0
    255 0 0
    128 0 0
    255 255 0
    128 128 0
    -1 -1 -1
    
    Expected output
    White
    Silver
    Gray
    Black
    Red
    Maroon
    Yellow
    Olive
    
  3. Example 3

    Input
    0 255 0
    0 128 0
    0 255 255
    0 128 128
    0 0 255
    0 0 128
    255 0 255
    128 0 128
    -1 -1 -1
    
    Expected output
    Lime
    Green
    Aqua
    Teal
    Blue
    Navy
    Fuchsia
    Purple