protected Colour determineWorkbookColour(Color awtColor)
{
//nearest match
int minDist = 999;
Colour minColour = null;
//nearest match among the available (not used) colours
int minDistAvailable = 999;
Colour minColourAvailable = null;
Colour[] colors = Colour.getAllColours();
for (int i = 0; i < colors.length; i++)
{
Colour colour = colors[i];
RGB customRGB = (RGB) usedColours.get(colour);
RGB rgb = customRGB == null ? colour.getDefaultRGB() : customRGB;
int dist = rgbDistance(awtColor, rgb);
if (dist < minDist)
{
minDist = dist;
minColour = colour;
}
if (dist == 0)//exact match
{
break;
}
if (customRGB == null)//the colour is not used
{
if (dist < minDistAvailable)
{
minDistAvailable = dist;
minColourAvailable = colour;
}
}
}
Colour workbookColour;
if (minDist == 0)//exact match found
{
if (!usedColours.containsKey(minColour))
{
//if the colour is not marked as used, mark it