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++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
#define EPS 1e-10
using namespace std;
typedef long long llong;
string color(int x) {
	switch (x) {
	case 0:return "black";
	case 1:return "blue";
	case 2:return "lime";
	case 3:return "aqua";
	case 4:return "red";
	case 5:return "fuchsia";
	case 6:return "yellow";
	case 7:return "white";
	default:return "gray";
	}
}

int h(char c) {
	if (isalpha(c))return (c - 'a')+10;
	else return (c - '0');
}

int main() {

	map<int, vector<double> >m;
	m[0].push_back(0x00);
	m[0].push_back(0x00);
	m[0].push_back(0x00);
	m[1].push_back(0x00);
	m[1].push_back(0x00);
	m[1].push_back(0xff);
	m[2].push_back(0x00);
	m[2].push_back(0xff);
	m[2].push_back(0x00);
	m[3].push_back(0x00);
	m[3].push_back(0xff);
	m[3].push_back(0xff);
	m[4].push_back(0xff);
	m[4].push_back(0x00);
	m[4].push_back(0x00);
	m[5].push_back(0xff);
	m[5].push_back(0x00);
	m[5].push_back(0xff);
	m[6].push_back(0xff);
	m[6].push_back(0xff);
	m[6].push_back(0x00);
	m[7].push_back(0xff);
	m[7].push_back(0xff);
	m[7].push_back(0xff);

	vector<double>d(8);

	string s;

	while (cin >> s, s != "0") {

		double r, g, b;

		r = h(s[1]) * 16 + h(s[2]); //cout << "red is " << r << endl;
		g = h(s[3]) * 16 + h(s[4]); //cout << "green is " << g << endl;
		b = h(s[5]) * 16 + h(s[6]); //cout << "blue is " << b << endl;

		for (int i = 0; i < 8; i++) {

			d[i] = pow(pow(r - m[i][0], 2) + pow(g - m[i][1], 2) + pow(b - m[i][2], 2), 0.5);
		//	cout << d[i] << " ";
		}//cout << endl;
		cout << color(distance(d.begin(),min_element(d.begin(), d.end()))) << endl;


	}



	return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
using namespace std;
int Overten(char a){
  if(a-'0'<10)return a-'0';
  if(a=='A')return 10;
  if(a=='B')return 11;
  if(a=='C')return 12;
  if(a=='D')return 13;
  if(a=='E')return 14;
  if(a=='F')return 15;
}
int changeDtoI(char a,char b){
  return Overten(a)*16+Overten(b);
}
int main(void){
  string color;
  while(cin>>color,color.size()>3){
    int R = changeDtoI(color[1],color[2])<128?0:1;
    int G = changeDtoI(color[3],color[4])<128?0:1;
    int B = changeDtoI(color[4],color[5])<128?0:1;
    //cout<<R << " " << G << " " << B <<endl;
    if(R * G * B)cout<<"white"<<endl;
    else if(R * G)cout<<"yellow"<<endl;
    else if(R * B)cout<<"fuchsia"<<endl;
    else if(G * B)cout<<"aqua"<<endl;
    else if(R)cout<<"red"<<endl;
    else if(G)cout<<"green"<<endl;
    else if(B)cout<<"blue"<<endl;
    else cout<<"black"<<endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

#define pb push_back
#define rep(i, a, n) for(int i = (a); i < (n); i++)
#define dep(i, a, n) for(int i = (a); i >= (n); i--)
#define mod 1e9+7

__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

int hen(char a) {
  if((int)a - 48 >= 0 && (int)a - 48 < 10) return (int)a - 48;
  if(a == 'a') return 10;
  else if(a == 'b') return 11;
  else if(a == 'c') return 12;
  else if(a == 'd') return 13;
  else if(a == 'e') return 14;
  else return 15;
}

int main() {
  string s;
  while(cin >> s) {
    if(s == "0") return 0;
    int r = hen(s[1]) * 16 + hen(s[2]);
    int g = hen(s[3]) * 16 + hen(s[4]);
    int b = hen(s[5]) * 16 + hen(s[6]);

    int ans = INT_MAX;
    string mans;
    if(r * r + g * g + b * b < ans) ans = r * r + g * g + b * b, mans = "brack";
    if(r * r + g * g + (b - 255) * (b - 255) < ans) ans = r * r + g * g + (b - 255) * (b - 255), mans = "blue";
    if(r * r + (g - 255) * (g - 255) + b * b < ans) ans = r * r + (g - 255) * (g - 255) + b * b, mans = "lime";
    if(r * r + (g - 255) * (g - 255) + (b - 255) * (b - 255) < ans) ans = r * r + (g - 255) * (g - 255) + (b - 255) * (b - 255), mans = "	aqua";
    if((r - 255) * (r - 255) + g * g + b * b < ans) ans = (r - 255) * (r - 255) + g * g + b * b, mans = "red";
    if((r - 255) * (r - 255) + g * g + (b - 255) * (b - 255) < ans) ans = (r - 255) * (r - 255) + g * g + (b - 255) * (b - 255), mans = "fuchsia";
    if((r - 255) * (r - 255) + (g - 255) * (g - 255) + b * b < ans) ans = (r - 255) * (r - 255) + (g - 255) * (g - 255) + b * b, mans = "yello";
    if((r - 255) * (r - 255) + (g - 255) * (g - 255) + (b - 255) * (b - 255) < ans) ans = (r - 255) * (r - 255) + (g - 255) * (g - 255) + (b - 255) * (b - 255), mans = "white";

    cout << mans << endl;
  }
}

C++: #include<bits/stdc++.h>
using namespace std;

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

int dis(int id, vector<int> a){
  int res = 0;
  for(int i=0;i<3;i++){
    int comp = 0;
    if(id&1) comp = 255;
    res += (a[i]-comp)*(a[i]-comp);
    id/=2;
  }
  return res;
}

int toDec(char a){
  if(isdigit(a))return a-'0';
  return a-'a'+10;
}

int main(){
  string s;
  while(cin >> s, s != "0"){
    vector<int> a(3);
    for(int i=0;i<3;i++){
      a[i] = toDec(s[2*i+1])*16 + toDec(s[2*i+2]);
    }
    int ans = 0, dmin = dis(0,a);
    for(int i=1;i<8;i++){
      if(dis(i,a) < dmin){
        ans = i; dmin = dis(i,a);
      }
    }
    cout << colors[ans] << endl;
  }
}