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 <string>
#include <cmath>

using namespace std;

int colorCalc(char c1, char c2)
{
	int color = 0;
	if('a' <= c1 && c1 <= 'f')
	{
		color += (c1 - 'a' + 10) * 16;
	}
	else
	{
		color += (c1 - '0') * 16;
	}
	if('a' <= c2 && c2 <= 'f')
	{
		color += c2 - 'a' + 10;
	}
	else
	{
		color += c2 - '0';
	}
	return(color);
}
void solve()
{
	int colors[9][4] = {{{0},   {0},   {0}}, 
						{{0},   {0},   {255}}, 
						{{0},   {255}, {0}}, 
						{{0},   {255}, {255}}, 
						{{255}, {0},   {0}}, 
						{{255}, {0},   {255}}, 
						{{255}, {255}, {0}}, 
						{{255}, {255}, {255}}};
	string name[9] = {"black", 
					  "blue", 
					  "lime", 
					  "aqua", 
					  "red", 
					  "fuchsia", 
					  "yellow", 
					  "white"};
	string str;
	while(cin >> str, str != "0")
	{
		int r = colorCalc(str[1], str[2]);
		int g = colorCalc(str[3], str[4]);
		int b = colorCalc(str[5], str[6]);
		int min = 999999999;
		int label = 0;
		for(int i = 0; i < 8; ++i)
		{
			int sum = sqrt((double)(r - colors[i][0]) * (r - colors[i][0]) + (g - colors[i][1]) * (g - colors[i][1]) + (b - colors[i][2]) * (b - colors[i][2]));
			if(sum < min)
			{
				min = sum;
				label = i;
			}
		}
		cout << name[label] << endl;
	}
}

int main()
{
	solve();
	return(0);
}

C++: #include<iostream>
#include<map>
#include<math.h>

using namespace std;

map<int, int> MR;
map<int, int> MG;
map<int, int> MB;

void make(){
  MR[0] = 0;
  MG[0] = 0;
  MB[0] = 0;
  MR[1] = 0;
  MG[1] = 0;
  MB[1] = 255;
  MR[2] = 0;
  MG[2] = 255;
  MB[2] = 0;
  MR[3] = 0;
  MG[3] = 255;
  MB[3] = 255;
  MR[4] = 255;
  MG[4] = 0;
  MB[4] = 0;
  MR[5] = 255;
  MG[5] = 0;
  MB[5] = 255;
  MR[6] = 255;
  MG[6] = 255;
  MB[6] = 0;
  MR[7] = 255;
  MG[7] = 255;
  MB[7] = 255;
}

int main(){

  make();

  char in[2];
  int status = 0;

  while(cin >> in[0]){
    if(in[0] == '0') break;

    int r, g, b;

    for(int i = 0; i < 3; i++){
      
      cin >> in[0] >> in[1];

      for(int j = 0; j <= 1; j++){
	if(in[j] == 'a') in[j] -= 39;
	if(in[j] == 'b') in[j] -= 39;
	if(in[j] == 'c') in[j] -= 39;
	if(in[j] == 'd') in[j] -= 39;
	if(in[j] == 'e') in[j] -= 39;
	if(in[j] == 'f') in[j] -= 39;
      }

      if(i == 0) r = (in[0] - 48) * 16 + (in[1] - 48);
      if(i == 1) g = (in[0] - 48) * 16 + (in[1] - 48);
      if(i == 2) b = (in[0] - 48) * 16 + (in[1] - 48);
    }

    double d;
    int min, then;

    for(int j = 0; j < 8; j++){
      d = sqrt((r - MR[j])*(r - MR[j])+(g - MG[j])*(g - MG[j])+(b - MB[j])*(b - MB[j]));
      if(j == 0 || min > d){
	min = d;
	then = j;
      }
    }

    if(then == 0) cout << "black" <<endl;
    if(then == 1) cout << "blue" <<endl;
    if(then == 2) cout << "lime" <<endl;
    if(then == 3) cout << "aqua" <<endl;
    if(then == 4) cout << "red" <<endl;
    if(then == 5) cout << "fuchsia" <<endl;
    if(then == 6) cout << "yellow" <<endl;
    if(then == 7) cout << "white" <<endl;

  }
  return 0;
}

C++: // AOJ 0176
#include<algorithm>
#include<iostream>
#include<sstream>
#include<string>
#include<utility>

typedef std::pair<int, int> P;

const int INF = 1 << 30;

const int r[8] = { 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff },
		  g[8] = { 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff },
		  b[8] = { 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff };

const std::string cols[8] = { "black", "blue", "lime", "aqua", "red", "fuchsia", "yellow", "white" };

int sqr( int x )
{
	return x * x;
}

int main()
{
	std::string s;
	while( std::cin >> s, s != "0" )
	{
		std::string t;
		for( int i = 1; i != s.size(); ++i )
		{
			t += s[i];
			if( i % 2 == 0 && i < 6 )
			{
				t += ' ';
			}
		}

		std::istringstream iss( t );
		int R, G, B;
		iss >> std::hex >> R >> G >> B;

		P ans( INF, INF );
		for( int i = 0; i != 8; ++i )
		{
			int d = sqr( R - r[i] ) + sqr( G - g[i] ) + sqr( B - b[i] );
			ans = std::min( ans, P( d, i ) );
		}

		std::cout << cols[ans.second] << std::endl;
	}

	return 0;
}

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

const int INF = 10000000;

int base16(char c) {
  if( 'a' <= c ) return c-'a'+10;
  else return c-'0';
}

int main() {

  string s;

  int coltabel[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 } };

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

  while(true) {

    cin>>s;
    if( s=="0" ) break;

    int r,g,b;
    r = base16(s[1])*16+base16(s[2]);
    g = base16(s[3])*16+base16(s[4]);
    b = base16(s[5])*16+base16(s[6]);

    int res,tmp=INF,t;

    for(int i=0; i<8; i++) {
      if( ( t = (r-coltabel[i][0])*(r-coltabel[i][0])
	  + (g-coltabel[i][1])*(g-coltabel[i][1])
	  + (b-coltabel[i][2])*(b-coltabel[i][2]) )
	  < tmp ) {
	//cout<<t<<" "<<i<<endl;
	res = i;
	tmp = t;
      }
    }

    cout<<nametable[res]<<endl;
  }
}

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

int R,G,B;
void solve() {
  string color[8] = {"black","blue","lime","aqua","red","fuchsia","yellow","white"};
  int icolor[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}};
  int nearest=0,dist,mindist=1000000;
  for(int i=0;i<8;i++) {
    dist =  (R-icolor[i][0])*(R-icolor[i][0]) + (G-icolor[i][1])*(G-icolor[i][1]) + (B-icolor[i][2])*(B-icolor[i][2]);
    if(dist < mindist) {
      mindist = dist;
      nearest = i;
    }
  }
  cout << color[nearest] << endl;
}

int main() {
  string str;
  while(getline(cin,str)) {
    if(str == "0") break;
    R = strtol(str.substr(1,2).c_str(),NULL,16);
    G = strtol(str.substr(3,2).c_str(),NULL,16);
    B = strtol(str.substr(5,2).c_str(),NULL,16);
    solve();
  }
  return 0;
}