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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
using namespace std;

const int INF = 1e9;
typedef long long ll;

struct Color{
  string name;
  int R, G, B;
};

Color list[8];

void init(){
  list[0].name = "black";   list[0].R = 0x00; list[0].G = 0x00; list[0].B = 0x00;
  list[1].name = "blue";    list[1].R = 0x00; list[1].G = 0x00; list[1].B = 0xff;
  list[2].name = "lime";    list[2].R = 0x00; list[2].G = 0xff; list[2].B = 0x00;
  list[3].name = "aqua";    list[3].R = 0x00; list[3].G = 0xff; list[3].B = 0xff;
  list[4].name = "red";     list[4].R = 0xff; list[4].G = 0x00; list[4].B = 0x00;
  list[5].name = "fuchsia"; list[5].R = 0xff; list[5].G = 0x00; list[5].B = 0xff;
  list[6].name = "yellow";  list[6].R = 0xff; list[6].G = 0xff; list[6].B = 0x00;
  list[7].name = "white";   list[7].R = 0xff; list[7].G = 0xff; list[7].B = 0xff;
}

int main(void){
  init();
  string colorcode;
  while(cin >> colorcode && colorcode != "0"){
    int r = stoi(string(colorcode, 1, 2), nullptr, 16);
    int g = stoi(string(colorcode, 3, 2), nullptr, 16);
    int b = stoi(string(colorcode, 5, 2), nullptr, 16);
    int n, mind = INF;
    for(int i = 0; i < 8; i++){
      int d = (r - list[i].R) * (r - list[i].R) + (g - list[i].G) * (g - list[i].G) + (b - list[i].B) * (b - list[i].B);
      if(d < mind){
        mind = d;
        n = i;
      }
    }
    cout << list[n].name << endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  string s;
  map<char,int>m;
  m['a']=10;
  m['b']=11;
  m['c']=12;
  m['d']=13;
  m['e']=14;
  m['f']=15;
  int col[8][3];
  col[0][0]=0;col[0][1]=0;col[0][2]=0;
  col[1][0]=0;col[1][1]=0;col[1][2]=255;
  col[2][0]=0;col[2][1]=255;col[2][2]=0;
  col[3][0]=0;col[3][1]=255;col[3][2]=255;
  col[4][0]=255;col[4][1]=0;col[4][2]=0;
  col[5][0]=255;col[5][1]=0;col[5][2]=255;
  col[6][0]=255;col[6][1]=255;col[6][2]=0;
  col[7][0]=255;col[7][1]=255;col[7][2]=255;
  while(cin>>s,s!="0"){
    int a,b,c,a1,a2;
    if(isdigit(s[1]))a1=s[1]-'0';
    else a1=m[s[1]];
    if(isdigit(s[2]))a2=s[2]-'0';
    else a2=m[s[2]];
    a=a1*16+a2;
    if(isdigit(s[3]))a1=s[3]-'0';
    else a1=m[s[3]];
    if(isdigit(s[4]))a2=s[4]-'0';
    else a2=m[s[4]];
    b=a1*16+a2;
    if(isdigit(s[5]))a1=s[5]-'0';
    else a1=m[s[5]];
    if(isdigit(s[6]))a2=s[6]-'0';
    else a2=m[s[6]];
    c=a1*16+a2;
    int p,ss=INT_MAX;
    for(int i=0;i<8;i++){
      int x=(col[i][0]-a)*(col[i][0]-a)+
            (col[i][1]-b)*(col[i][1]-b)+
            (col[i][2]-c)*(col[i][2]-c);
      if(x<ss){
        ss=x;
        p=i;
      }
    }
    if(p==0)cout<<"black"<<endl;
    if(p==1)cout<<"blue"<<endl;
    if(p==2)cout<<"lime"<<endl;
    if(p==3)cout<<"aqua"<<endl;
    if(p==4)cout<<"red"<<endl;
    if(p==5)cout<<"fuchsia"<<endl;
    if(p==6)cout<<"yellow"<<endl;
    if(p==7)cout<<"white"<<endl;
  }
}

C++: #include <iostream>
#include <cstdio>
using namespace std;
int HtoD(char c){
	if('0'<=c&&c<='9')return c-'0';
	if('A'<=c&&c<='F')return c-'A'+10;
	if('a'<=c&&c<='f')return c-'a'+10;
	return 0;
}
int main(){
	const int BCOL[8][3]={{0,0,0},{0,0,255},{0,255,0},{0,255,255},{255,0,0},{255,0,255},{255,255,0},{255,255,255}};
	const string BCOLN[8]={"black","blue","lime","aqua","red","fuchsia","yellow","white"};
	string s;
	while(cin>>s){
		if(s[0]=='0')break;
		int r=(HtoD(s[1])<<4)+HtoD(s[2]),g=(HtoD(s[3])<<4)+HtoD(s[4]),b=(HtoD(s[5])<<4)+HtoD(s[6]);
		int mind=195075;
		int outn=0;
		for(int i=7;i>=0;--i){
			int d = (r-BCOL[i][0])*(r-BCOL[i][0])+(g-BCOL[i][1])*(g-BCOL[i][1])+(b-BCOL[i][2])*(b-BCOL[i][2]);
			if(d<=mind){
				mind = d;
				outn = i;
			}
		}
		cout<<BCOLN[outn]<<"\n";
	}
	return 0;
}

C++: #include<iostream>
#include<string>
using namespace std;
string S, x[2][2][2] = { {{"black","blue"}, {"lime","aqua"}}, { {"red","fuchsia"} {"yellow","white"} } };
char T[9] = "89abcdef";
int a, b, c;
int main() {
	while (true) {
		a = 0; b = 0; c = 0;
		cin >> S;
		if (S == "0") { break; }
		for (int i = 0; i < 8; i++){
			if (T[i] == S[1]) {
				a = 1;
			}
		}
		for (int i = 0; i < 8; i++) {
			if (T[i] == S[3]) {
				b = 1;
			}
		}
		for (int i = 0; i < 8; i++) {
			if (T[i] == S[5]) {
				c = 1;
			}
		}
		cout << x[a][b][c] << endl;
	}
}

C++: #include <stdio.h>

const char *t[] = {"black","blue","lime","aqua","redff","fuchsia","yellow","white"};

int solve(int r, int g, int b){
    int min = 0; int mind = 200000;
    for(int i = 0; i < 8; i++){
        int r0 = ((i>>2)&1)*0xff, g0 = ((i>>1)&1)*0xff, b0 = ((i>>0)&1)*0xff;
        //pf("%d,%d,%d : %d,%d,%d\n", r,g,b,r0,g0,b0);
        int d = (r-r0)*(r-r0)+(g-g0)*(g-g0)+(b-b0)*(b-b0);
        if(mind > d){
            mind = d, min = i;
        }
    }
    if(min >= 8) throw;
    return min;
}

int main(){
    int n;
    char buf[16];
    while(fgets(buf,sizeof(buf),stdin)){
        if(buf[0] == '0') break;
        sscanf(buf+1,"%x",&n);
        int i = solve((n>>16)&0xff,(n>>8)&0xff,n&0xff);
        printf("%s\n", t[i]);
    }
    return 0;
}