http://blog.csdn.net/hjh2005/article/details/8821052
有些时候我们需要为一些对象附上随机的颜色,比如我们有这么一个需求,在一个chart里添加显示曲线,刚开始曲线的颜色默认都是黑色的很不好看,后来为了显示的美观我们想给添加的曲线随机的附上颜色,但是有一个要求,曲线的颜色不能太淡,比如不能是白色。因为我们的chart的背景颜色是白色的,如果曲线也是白色那曲线就会看不到了。
我们首先想到的方法是如下:
Color c(rand()%256,rand()%256,rand()%256);
这样可以实现我们对随机颜色的要求,但是不满足我们不能为白色的要求,为了避免白色,我们在对这个颜色进行检查,如果r、g、b分量的值都超过230,表示颜色太淡重新随机,但是这样的方法总让人感觉不那么舒服。
后来想到了在HSL颜色空间里做文章是否会更舒服呢?
于是通过Wiki复习HSL颜色空间的知识。发现在HSL空间里如果L分量大于200,颜色看起来就比较淡了,所以我们可以随机生成小于200的数值作为L分量,再借助强大的Qt于是我们可以这样实现我们的需求:
首先借助Qt的QColor生成一个颜色对象:
QColor qc=QColor::fromHsl(rand()%360,rand()%256,rand()%200);
这里要注意的是H分量的值域是0到359的。
最后得到的颜色为:
Color c(qc.red(),qc.green(),qc.blue());
如果不用Qt的话,网上有很多HSL颜色空间转RGB颜色空间的代码和公式也可是替代上面用到的Qt。这样我们就省略了第一种方法里的循环,实现的方法看起来更加舒服了。
有些时候我们可能会有这样的需求,比如我们想给一张地图上色,相邻的国家的颜色视觉区别要尽可能大,于是我们给的一种或几种颜色,要找到与这些颜色差别最大的颜色,这要怎么实现呢?下面是别人写的代码。我觉得还是有改进的空间的:
- static ColorType getUniqueColor(const std::vector<ColorType>& excludedColors)
- {
- unsigned int i,j,k;
- ColorType uniqueColor(0,0,0);
- //如果当前没有颜色
- if (excludedColors.size()==0)
- {
- return uniqueColor; //因为没有颜色所以随便返回一个颜色
- }
- //如果当前只有一个颜色
- if (excludedColors.size()==1)
- {
- int maxDist=-1;
- int red=excludedColors[0].mRed;
- int green=excludedColors[0].mGreen;
- int blue=excludedColors[0].mBlue;
- for (i=0;i<256;i+=255)
- {
- for (j=0;j<256;j+=255)
- {
- for (k=0;k<256;k+=255)
- {
- int dist=(i-red)*(i-red)+(j-green)*(j-green)+(k-blue)*(k-blue);
- if (dist>maxDist)
- {
- maxDist=dist;
- uniqueColor.mRed=i;
- uniqueColor.mGreen=j;
- uniqueColor.mBlue=k;
- }
- }
- }
- }
- return uniqueColor;
- }
- std::vector<unsigned int> badColors;
- badColors.reserve(excludedColors.size()); //预留空间
- std::vector<ColorType>::const_iterator iter;
- for (iter=excludedColors.begin();iter!=excludedColors.end();iter++)
- {
- badColors.push_back((iter->mBlue<<16)+(iter->mGreen<<8)+iter->mRed);
- }
- std::sort(badColors.begin(),badColors.end());
- unsigned int duplicates=0;
- unsigned int next;
- for (i=0,next=1;i<badColors.size()-duplicates;i++)
- {
- for (j = next; j < badColors.size(); j++)
- {
- if (badColors[i] != badColors[j])
- {
- badColors[i + 1] = badColors[j];
- next = j + 1;
- break;
- }
- else
- {
- duplicates++;
- }
- }
- }
- badColors.erase(badColors.begin() + (badColors.size() - duplicates), badColors.end());
- std::vector<unsigned int>::iterator ulit = badColors.begin();
- unsigned int testColor;
- for (testColor = 0; testColor < 0xffffff; testColor++)
- {
- if (testColor == *ulit)
- {
- ulit++;
- }
- else
- {
- break;
- }
- }
- if (testColor == 0x01000000) // 如果搜遍了16.7百万的颜色都没找到的话,则返回无效的颜色
- {
- uniqueColor = ColorType();
- }
- else
- {
- uniqueColor.mBlue = (testColor&0xff0000)>>16;
- uniqueColor.mGreen = (testColor&0xff00)>>8;
- uniqueColor.mRed = testColor&0xff;
- }
- return uniqueColor;
- }
ColorType是颜色类型,里面包含了三个分量。
如果我们要同时获取多个不同的颜色呢?可以参考下面的代码:
- /**
- * 产生一个或多个唯一的颜色
- * @param count 要产生的颜色的个数
- * @param colors 用于保存生成颜色的向量
- * @param excludeColors 要排除的颜色
- * @return 产生的颜色的个数
- */
- static unsigned int getUniqueColors(unsigned int count, std::vector<ColorType>& colors,
- const std::vector<ColorType>& excludeColors)
- {
- unsigned int i, j, k, l;
- unsigned int numUnique = 0;
- double slValues[] = {0.0, 1.0, 0.5, 0.8, 0.3, 0.6, 0.9, 0.2, 0.7, 0.4, 0.1};
- ColorType baseColors[] =
- {
- ColorType(0,0,255),
- ColorType(0,255,0),
- ColorType(255,0,0),
- ColorType(0,255,255),
- ColorType(255,255,0),
- ColorType(255,0,255),
- ColorType(255,255,255)
- };
- for (i = 0; i < sizeof(slValues) / sizeof(slValues[0]); i++)
- {
- for (j = 0; j < sizeof(slValues) / sizeof(slValues[0]); j++)
- {
- for (k = 0; k < sizeof(baseColors) / sizeof(baseColors[0]); k++)
- {
- int newColor[3];
- int maxValue;
- newColor[0] = (int) (baseColors[k].mRed * slValues[j] + 0.5);
- newColor[1] = (int) (baseColors[k].mGreen * slValues[j] + 0.5);
- newColor[2] = (int) (baseColors[k].mBlue * slValues[j] + 0.5);
- maxValue = 0;
- for (l = 0; l < 3; l++)
- {
- if (newColor[l] > maxValue)
- {
- maxValue = newColor[l];
- }
- }
- maxValue = (int) (maxValue * slValues[i] + 0.5);
- for (l = 0; l < 3; l++)
- {
- if (newColor[l] < maxValue)
- {
- newColor[l] = maxValue;
- }
- }
- ColorType colorToInsert;
- colorToInsert.mRed = newColor[0];
- colorToInsert.mGreen = newColor[1];
- colorToInsert.mBlue = newColor[2];
- for (l=0; l<excludeColors.size(); l++)
- {
- if (excludeColors[l].mRed == colorToInsert.mRed &&
- excludeColors[l].mGreen == colorToInsert.mGreen &&
- excludeColors[l].mBlue == colorToInsert.mBlue)
- {
- break;
- }
- }
- if (l == excludeColors.size())
- {
- for (l = 0; l < colors.size(); l++)
- {
- if (colors[l].mRed == colorToInsert.mRed &&
- colors[l].mGreen == colorToInsert.mGreen &&
- colors[l].mBlue == colorToInsert.mBlue)
- {
- break;
- }
- }
- if (l == colors.size())
- {
- colors.push_back (colorToInsert);
- ++numUnique;
- if (colors.size() == count)
- {
- return numUnique;
- }
- }
- }
- }
- }
- }
- return numUnique;
- }