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 <bits/stdc++.h>
using namespace std;
int main(void){
    string s;
    while (cin>>s) {
        if (s=="0") {
            break;
        } else {
            int r=0,g=0,b=0;
            double min=100000.0;
            for (int i=1;i<7;i++) {
                if (i==1) {
                    if (s.at(i)=='a') {
                        r=r+16*10;
                    } else if (s.at(i)=='b') {
                        r=r+16*11;
                    } else if (s.at(i)=='c') {
                        r=r+16*12;
                    } else if (s.at(i)=='d') {
                        r=r+16*13;
                    } else if (s.at(i)=='e') {
                        r=r+16*14;
                    } else if (s.at(i)=='f') {
                        r=r+16*15;
                    } else if (s.at(i)=='9') {
                        r=r+16*9;
                    } else if (s.at(i)=='8') {
                        r=r+16*8;
                    } else if (s.at(i)=='7') {
                        r=r+16*7;
                    } else if (s.at(i)=='6') {
                        r=r+16*6;
                    } else if (s.at(i)=='5') {
                        r=r+16*5;
                    } else if (s.at(i)=='4') {
                        r=r+16*4;
                    } else if (s.at(i)=='3') {
                        r=r+16*3;
                    } else if (s.at(i)=='2') {
                        r=r+16*2;
                    } else if (s.at(i)=='1') {
                        r=r+16*1;
                    } else if (s.at(i)=='0') {
                        r=r+16*0;
                    }
                } else if (i==2) {
                    if (s.at(i)=='a') {
                        r=r+10;
                    } else if (s.at(i)=='b') {
                        r=r+11;
                    } else if (s.at(i)=='c') {
                        r=r+12;
                    } else if (s.at(i)=='d') {
                        r=r+13;
                    } else if (s.at(i)=='e') {
                        r=r+14;
                    } else if (s.at(i)=='f') {
                        r=r+15;
                    } else if (s.at(i)=='9') {
                        r=r+9;
                    } else if (s.at(i)=='8') {
                        r=r+8;
                    } else if (s.at(i)=='7') {
                        r=r+7;
                    } else if (s.at(i)=='6') {
                        r=r+6;
                    } else if (s.at(i)=='5') {
                        r=r+5;
                    } else if (s.at(i)=='4') {
                        r=r+4;
                    } else if (s.at(i)=='3') {
                        r=r+3;
                    } else if (s.at(i)=='2') {
                        r=r+2;
                    } else if (s.at(i)=='1') {
                        r=r+1;
                    } else if (s.at(i)=='0') {
                        r=r+0;
                    }
                } else if (i==3) {
                    if (s.at(i)=='a') {
                        g=g+16*10;
                    } else if (s.at(i)=='b') {
                        g=g+16*11;
                    } else if (s.at(i)=='c') {
                        g=g+16*12;
                    } else if (s.at(i)=='d') {
                        g=g+16*13;
                    } else if (s.at(i)=='e') {
                        g=g+16*14;
                    } else if (s.at(i)=='f') {
                        g=g+16*15;
                    } else if (s.at(i)=='9') {
                        g=g+16*9;
                    } else if (s.at(i)=='8') {
                        g=g+16*8;
                    } else if (s.at(i)=='7') {
                        g=g+16*7;
                    } else if (s.at(i)=='6') {
                        g=g+16*6;
                    } else if (s.at(i)=='5') {
                        g=g+16*5;
                    } else if (s.at(i)=='4') {
                        g=g+16*4;
                    } else if (s.at(i)=='3') {
                        g=g+16*3;
                    } else if (s.at(i)=='2') {
                        g=g+16*2;
                    } else if (s.at(i)=='1') {
                        g=g+16*1;
                    } else if (s.at(i)=='0') {
                        g=g+16*0;
                    }
                } else if (i==4) {
                    if (s.at(i)=='a') {
                        g=g+10;
                    } else if (s.at(i)=='b') {
                        g=g+11;
                    } else if (s.at(i)=='c') {
                        g=g+12;
                    } else if (s.at(i)=='d') {
                        g=g+13;
                    } else if (s.at(i)=='e') {
                        g=g+14;
                    } else if (s.at(i)=='f') {
                        g=g+15;
                    } else if (s.at(i)=='9') {
                        g=g+9;
                    } else if (s.at(i)=='8') {
                        g=g+8;
                    } else if (s.at(i)=='7') {
                        g=g+7;
                    } else if (s.at(i)=='6') {
                        g=g+6;
                    } else if (s.at(i)=='5') {
                        g=g+5;
                    } else if (s.at(i)=='4') {
                        g=g+4;
                    } else if (s.at(i)=='3') {
                        g=g+3;
                    } else if (s.at(i)=='2') {
                        g=g+2;
                    } else if (s.at(i)=='1') {
                        g=g+1;
                    } else if (s.at(i)=='0') {
                        g=g+0;
                    }
                } else if (i==5) {
                    if (s.at(i)=='a') {
                        b=b+16*10;
                    } else if (s.at(i)=='b') {
                        b=b+16*11;
                    } else if (s.at(i)=='c') {
                        b=b+16*12;
                    } else if (s.at(i)=='d') {
                        b=b+16*13;
                    } else if (s.at(i)=='e') {
                        b=b+16*14;
                    } else if (s.at(i)=='f') {
                        b=b+16*15;
                    } else if (s.at(i)=='9') {
                        b=b+16*9;
                    } else if (s.at(i)=='8') {
                        b=b+16*8;
                    } else if (s.at(i)=='7') {
                        b=b+16*7;
                    } else if (s.at(i)=='6') {
                        b=b+16*6;
                    } else if (s.at(i)=='5') {
                        b=b+16*5;
                    } else if (s.at(i)=='4') {
                        b=b+16*4;
                    } else if (s.at(i)=='3') {
                        b=b+16*3;
                    } else if (s.at(i)=='2') {
                        b=b+16*2;
                    } else if (s.at(i)=='1') {
                        b=b+16*1;
                    } else if (s.at(i)=='0') {
                        b=b+16*0;
                    }
                } else if (i==6) {
                    if (s.at(i)=='a') {
                        b=b+10;
                    } else if (s.at(i)=='b') {
                        b=b+11;
                    } else if (s.at(i)=='c') {
                        b=b+12;
                    } else if (s.at(i)=='d') {
                        b=b+13;
                    } else if (s.at(i)=='e') {
                        b=b+14;
                    } else if (s.at(i)=='f') {
                        b=b+15;
                    } else if (s.at(i)=='9') {
                        b=b+9;
                    } else if (s.at(i)=='8') {
                        b=b+8;
                    } else if (s.at(i)=='7') {
                        b=b+7;
                    } else if (s.at(i)=='6') {
                        b=b+6;
                    } else if (s.at(i)=='5') {
                        b=b+5;
                    } else if (s.at(i)=='4') {
                        b=b+4;
                    } else if (s.at(i)=='3') {
                        b=b+3;
                    } else if (s.at(i)=='2') {
                        b=b+2;
                    } else if (s.at(i)=='1') {
                        b=b+1;
                    } else if (s.at(i)=='0') {
                        b=b+0;
                    }
                }
            }
            string color;
                double black=sqrt( (0-r)*(0-r)+(0-g)*(0-g)+(0-b)*(0-b) );
                if (black<min) {
                    min=black;
                    color="black";
                }
                double blue=sqrt( (0-r)*(0-r)+(0-g)*(0-g)+(255-b)*(255-b) );
                if (blue<min) {
                    min=blue;
                    color="blue";
                }
                double lime=sqrt( (0-r)*(0-r)+(255-g)*(255-g)+(0-b)*(0-b) );
                if (lime<min) {
                    min=lime;
                    color="lime";
                }
                double aqua=sqrt( (0-r)*(0-r)+(255-g)*(255-g)+(255-b)*(255-b) );
                if (aqua<min) {
                    min=aqua;
                    color="aqua";
                }
                double red=sqrt( (255-r)*(255-r)+(0-g)*(0-g)+(0-b)*(0-b) );
                if (red<min) {
                    min=red;
                    color="red";
                }
                double fuchsia=sqrt( (255-r)*(255-r)+(0-g)*(0-g)+(255-b)*(255-b) );
                if (fuchsia<min) {
                    min=fuchsia;
                    color="fuchsia";
                }
                double yellow=sqrt( (255-r)*(255-r)+(255-g)*(255-g)+(0-b)*(0-b) );
                if (yellow<min) {
                    min=yellow;
                    color="yellow";
                }
                double white=sqrt( (255-r)*(255-r)+(255-g)*(255-g)+(255-b)*(255-b) );
                if (white<min) {
                    min=white;
                    color="white";
                }
                cout<<color<<endl;
        }
    }
}