生活随笔
收集整理的這篇文章主要介紹了
随机颜色的生成
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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。這樣我們就省略了第一種方法里的循環,實現的方法看起來更加舒服了。
有些時候我們可能會有這樣的需求,比如我們想給一張地圖上色,相鄰的國家的顏色視覺區別要盡可能大,于是我們給的一種或幾種顏色,要找到與這些顏色差別最大的顏色,這要怎么實現呢?下面是別人寫的代碼。我覺得還是有改進的空間的:
[cpp]?view plaincopy
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)???? ????????{?? ????????????uniqueColor?=?ColorType();?? ????????}?? ????????else?? ????????{?? ????????????uniqueColor.mBlue?=?(testColor&0xff0000)>>16;?? ????????????uniqueColor.mGreen?=?(testColor&0xff00)>>8;?? ????????????uniqueColor.mRed?=?testColor&0xff;?? ????????}?? ?? ????????return?uniqueColor;?? ????}?? ColorType是顏色類型,里面包含了三個分量。
如果我們要同時獲取多個不同的顏色呢?可以參考下面的代碼:
[cpp]?view plaincopy
? ? ? ? ? ? ?? ???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;?? ????} ?
轉載于:https://www.cnblogs.com/lgh1992314/p/5834797.html
總結
以上是生活随笔為你收集整理的随机颜色的生成的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。