Go: package main

import (
	"fmt"
	"math"
	"strconv"
)

// ColorCode is a string like #rrggbb
type ColorCode string

// ColorName is a name of color
type ColorName string

// Color has colorname and colorcode
type Color struct {
	name ColorName
	code ColorCode
}

// ColorTable has default colors
type ColorTable struct {
	colors []Color
}

var (
	colorTable = ColorTable{
		[]Color{
			Color{"black", "#000000"},
			Color{"blue", "#0000ff"},
			Color{"lime", "#00ff00"},
			Color{"aqua", "#00ffff"},
			Color{"red", "#ff0000"},
			Color{"fuchsia", "#ff00ff"},
			Color{"yellow", "#ffff00"},
			Color{"white", "#ffffff"},
		},
	}
)

func main() {
	var code ColorCode
	fmt.Scanf("%s", &code)
	for {
		if code == "0" {
			break
		}
		name := colorTable.getNearestColor(code)
		fmt.Println(name)
		fmt.Scanf("%s", &code)
	}
}

func (colorstr ColorCode) parseColorValue() (float64, float64, float64) {
	rr, gg, bb := colorstr[1:3], colorstr[3:5], colorstr[5:7]
	rri, err1 := strconv.ParseUint(string(rr), 16, 8)
	ggi, err2 := strconv.ParseUint(string(gg), 16, 8)
	bbi, err3 := strconv.ParseUint(string(bb), 16, 8)
	if err1 != nil || err2 != nil || err3 != nil {
		err := fmt.Errorf(
			"Failed to read color strings, color code:%s, err: %v %v %v",
			colorstr, err1, err2, err3)
		panic(err)
	}
	return float64(rri), float64(ggi), float64(bbi)
}

func (table *ColorTable) getNearestColor(targetcode ColorCode) ColorName {
	var nearestColor ColorName
	var nearestValue = 1e5
	for _, v := range table.colors {
		trr, tgg, tbb := targetcode.parseColorValue()
		prr, pgg, pbb := v.code.parseColorValue()
		// diff := math.Sqrt(
		// 	math.Pow(prr-trr, 2) + math.Pow(pgg-tgg, 2) + math.Pow(pbb-tbb, 2))
		diff := math.Pow(prr-trr, 2) +
			math.Pow(pgg-tgg, 2) +
			math.Pow(pbb-tbb, 2)
		if nearestValue > diff {
			nearestValue = diff
			nearestColor = v.name
		}
	}
	return nearestColor
}

C++: #include <iostream>
#include <cstring>
#include <string>
using namespace std;

const int INF = 1 << 28;

int decimal(char a, char b) {
    int d;
    if (isdigit(a)) d += a - '0';
    else d += a - 'a' + 10;
    d *= 16;

    if (isdigit(b)) d += b - '0';
    else d += b - 'a' + 10;
    
    return d;
}

struct color_t {
    int r, g, b;
    color_t(int r=0, int g=0, int b=0) : r(r), g(g), b(b) {}
    color_t(string s) {
        r = decimal(s[1], s[2]);
        g = decimal(s[3], s[4]);
        b = decimal(s[5], s[6]);
    }
};

int square_distance(color_t a, color_t b) {
    return (a.r - b.r) * (a.r - b.r) + (a.g - b.g) * (a.g - b.g) + (a.b - b.b) * (a.b - b.b);
}

int main() {
    string color[8];
    color_t color_v[8];

    color[0] = "black";   color_v[0] = color_t("#000000");
    color[1] = "blue";    color_v[1] = color_t("#0000ff");
    color[2] = "lime";    color_v[2] = color_t("#00ff00");
    color[3] = "aqua";    color_v[3] = color_t("#00ffff");
    color[4] = "red";     color_v[4] = color_t("#ff0000");
    color[5] = "fuchsia"; color_v[5] = color_t("#ff00ff");
    color[6] = "yellow";  color_v[6] = color_t("#ffff00");
    color[7] = "white";   color_v[7] = color_t("#ffffff");

    string s;
    while (getline(cin, s)) {
        if (s == "0") break;
        color_t c(s);
        int min_sq_dist = INF;
        string min_dist_color_name;
        for (int i = 0; i < 8; i++) {
            int sq_dist = square_distance(color_v[i], c);
            //cout << color[i] << ' ' << sq_dist << endl;
            if (min_sq_dist > sq_dist) {
                min_sq_dist = sq_dist;
                min_dist_color_name = color[i];
            }
        }
        cout << min_dist_color_name << endl;
    }
    return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <climits>
using namespace std;

const static int COLOR_NAME_LENGTH = 7;
const static int BASE_COLOR_NUM = 8;
const static int MASK = 0x0000ff;

const int base_colors[ BASE_COLOR_NUM ] = {
  0x000000,
  0x0000ff,
  0x00ff00,
  0x00ffff,
  0xff0000,
  0xff00ff,
  0xffff00,
  0xffffff
};

const char *base_color_names[ BASE_COLOR_NUM ] = {
  "black",
  "blue",
  "lime",
  "aqua",
  "red",
  "fuchsia",
  "yellow",
  "white"
};

int diffsq( const int color1, const int color2 ) {
  int c1 = color1;
  int c2 = color2;
  int rdiff = ( ( c1 >> 16 ) & MASK ) - ( ( c2 >> 16 ) & MASK );
  int gdiff = ( ( c1 >>  8 ) & MASK ) - ( ( c2 >>  8 ) & MASK );
  int bdiff = ( ( c1       ) & MASK ) - ( ( c2       ) & MASK );

  return rdiff * rdiff + gdiff * gdiff + bdiff * bdiff;
}

int main() {

  while( 1 ) {
    char color_name[ COLOR_NAME_LENGTH + 1 ];
    scanf( "%s", color_name );
    if( color_name[ 0 ] == '0' ) {
      return EXIT_SUCCESS;
    }
    for( int i = 1; i < COLOR_NAME_LENGTH + 1; i++ )  {
      color_name[ i - 1 ] = color_name[ i ];
    }
    int color;
    int minidx = 0;
    int mindiff = INT_MAX;
    sscanf( color_name, "%x", &color );
    for( int i = 0; i < BASE_COLOR_NUM; i++ ) {
      int diff = diffsq( color, base_colors[ i ] );
      if( diff < mindiff ) {
        minidx = i;
        mindiff =  diff;
      }
    }
    printf( "%s\n", base_color_names[ minidx ] );
  }
}

C++: #include<iostream>
#include<cstdio>
#include<climits>
using namespace std;

int main(){
	char pn[8][10] = {"black","blue","lime","aqua","red","fuchsia","yellow","white"};
	int pR[8] = {0x00,0x00,0x00,0x00,0xff,0xff,0xff,0xff},
		pG[8] = {0x00,0x00,0xff,0xff,0x00,0x00,0xff,0xff},
		pB[8] = {0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff};
	char c;
	while(scanf(" %c",&c),c!='0'){
		int N; scanf("%x",&N);
		int R = N/(1<<16)%(16*16), G = N/(1<<8)%(16*16), B = N/(1<<0)%(16*16);
		int min_ = INT_MAX,ans;
		for(int i=0;i<8;i++){
			int dR = pR[i]-R, dG = pG[i]-G, dB = pB[i]-B;
			int d = dR*dR+dG*dG+dB*dB;
			if(d<min_){
				min_ = d; ans = i;
			}
		}
		printf("%s\n",pn[ans]);
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
using namespace std;

#define sq(x) ((x)*(x))

string color_name[8] = {
	"black",
	"blue",
	"lime",
	"aqua",
	"red",
	"fuchsia",
	"yellow",
	"white"
};

int color_rgb[8][3] = {
	{ 0x00, 0x00, 0x00 },
	{ 0x00, 0x00, 0xff },
	{ 0x00, 0xff, 0x00 },
	{ 0x00, 0xff, 0xff },
	{ 0xff, 0x00, 0x00 },
	{ 0xff, 0x00, 0xff },
	{ 0xff, 0xff, 0x00 },
	{ 0xff, 0xff, 0xff },
};

int main( void )
{
	string input;
	while ( 1 )
	{
		cin >> input;
		if ( input == "0" ) break;
		int r, g, b;
		sscanf( input.c_str(), "#%2x%2x%2x", &r, &g, &b );

		int min_value = 1 << 28, min_no = 0;
		for ( int i = 0; i < 8; i++ )
		{
			int d = sq(r-color_rgb[i][0]) + sq( g-color_rgb[i][1] ) + sq(b-color_rgb[i][2]);
			if ( d < min_value )
			{
				min_value = d;
				min_no = i;
			}
		}

		cout << color_name[min_no] << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;

string table[] = {"black","blue","lime","aqua","red","fuchsia","yellow","white"};

int to(char c){
	if( c >= 'a' ) return 10 + c - 'a';
	return c - '0';
}

int main(){
	string s;
	while(cin >> s && s != "0"){
		int r = to(s[1])*16+to(s[2]);
		int g = to(s[3])*16+to(s[4]);
		int b = to(s[5])*16+to(s[6]);
		vector< pair<int,int> > answer;
		for(int i = 0 ; i < 8 ; i++){
			int R = (i>>2&1)*255;
			int G = (i>>1&1)*255;
			int B = (i&1)*255;
			int dist = (R-r)*(R-r) + (g-G)*(g-G) + (b-B)*(b-B);
			//cout << R <<" " << G << " " << B << " " <<  endl;
			answer.push_back( make_pair(dist,i) );
		}
		cout << table[(*min_element(answer.begin(),answer.end())).second] << endl;
	}
}