### 用于创建色轮的功能

#### 8 个答案:

n = 10


ColorBrewer - 设计为可在地图上最大限度区分的颜色集。

Escaping RGBland: Selecting Colors for Statistical Graphics - 一份技术报告，描述了一组用于在hcl颜色空间中生成良好（即最大可区分）颜色集的算法。

class cColorPicker
{
public:
void Pick( vector<DWORD>&v_picked_cols, int count, int bright = 50 );
private:
DWORD HSL2RGB( int h, int s, int v );
unsigned char ToRGB1(float rm1, float rm2, float rh);
};
/**

Evenly allocate RGB colors around HSL color wheel

@param[out] v_picked_cols  a vector of colors in RGB format
@param[in]  count   number of colors required
@param[in]  bright  0 is all black, 100 is all white, defaults to 50

based on Fig 3 of http://epub.wu-wien.ac.at/dyn/virlib/wp/eng/mediate/epub-wu-01_c87.pdf?ID=epub-wu-01_c87

*/

void cColorPicker::Pick( vector<DWORD>&v_picked_cols, int count, int bright )
{
v_picked_cols.clear();
for( int k_hue = 0; k_hue < 360; k_hue += 360/count )
v_picked_cols.push_back( HSL2RGB( k_hue, 100, bright ) );
}
/**

Convert HSL to RGB

based on http://www.codeguru.com/code/legacy/gdi/colorapp_src.zip

*/

DWORD cColorPicker::HSL2RGB( int h, int s, int l )
{
DWORD ret = 0;
unsigned char r,g,b;

float saturation = s / 100.0f;
float luminance = l / 100.f;
float hue = (float)h;

if (saturation == 0.0)
{
r = g = b = unsigned char(luminance * 255.0);
}
else
{
float rm1, rm2;

if (luminance <= 0.5f) rm2 = luminance + luminance * saturation;
else                     rm2 = luminance + saturation - luminance * saturation;
rm1 = 2.0f * luminance - rm2;
r   = ToRGB1(rm1, rm2, hue + 120.0f);
g = ToRGB1(rm1, rm2, hue);
b  = ToRGB1(rm1, rm2, hue - 120.0f);
}

ret = ((DWORD)(((BYTE)(r)|((WORD)((BYTE)(g))<<8))|(((DWORD)(BYTE)(b))<<16)));

return ret;
}

unsigned char cColorPicker::ToRGB1(float rm1, float rm2, float rh)
{
if      (rh > 360.0f) rh -= 360.0f;
else if (rh <   0.0f) rh += 360.0f;

if      (rh <  60.0f) rm1 = rm1 + (rm2 - rm1) * rh / 60.0f;
else if (rh < 180.0f) rm1 = rm2;
else if (rh < 240.0f) rm1 = rm1 + (rm2 - rm1) * (240.0f - rh) / 60.0f;

return static_cast<unsigned char>(rm1 * 255);
}

int _tmain(int argc, _TCHAR* argv[])
{
vector<DWORD> myCols;
cColorPicker colpick;
colpick.Pick( myCols, 20 );
for( int k = 0; k < (int)myCols.size(); k++ )
printf("%d: %d %d %d\n", k+1,
( myCols[k] & 0xFF0000 ) >>16,
( myCols[k] & 0xFF00 ) >>8,
( myCols[k] & 0xFF ) );

return 0;
}


function random_color($i = null,$n = 10, $sat = .5,$br = .7) {
$i = is_null($i) ? mt_rand(0,$n) :$i;
$rgb = hsv2rgb(array($i*(360/$n),$sat, $br)); for ($i=0 ; $i<=2 ;$i++)
$rgb[$i] = dechex(ceil($rgb[$i]));
return implode('', $rgb); } function hsv2rgb($c) {
list($h,$s,$v)=$c;
if ($s==0) return array($v,$v,$v);
else {
$h=($h%=360)/60;
$i=floor($h);
$f=$h-$i;$q[0]=$q[1]=$v*(1-$s);$q[2]=$v*(1-$s*(1-$f));$q[3]=$q[4]=$v;
$q[5]=$v*(1-$s*$f);
return(array($q[($i+4)%6]*255,$q[($i+2)%6]*255,$q[$i%6]*255)); //[1]
}
}


（注意，前面的伪代码不一致）

//colors entered as 0-255 [R, G, B]
colors = []; //holds final colors to be used
rand = new Random();

//assumes n is less than 16,777,216
randomGen(int n){
while (len(colors) < n){
//generate a random number between 0,255 for each color
newRed = rand.next(256);
newGreen = rand.next(256);
newBlue = rand.next(256);
temp = [newRed, newGreen, newBlue];
//only adds new colors to the array
if temp not in colors {
colors.append(temp);
}
}
}


for item in color{
itemSq = (item[0]^2 + item[1]^2 + item[2]^2])^(.5);
tempSq = (temp[0]^2 + temp[1]^2 + temp[2]^2])^(.5);
dist = itemSq - tempSq;
dist = abs(dist);
}
//NUMBER can be your chosen distance apart.
if dist < NUMBER and temp not in colors {
colors.append(temp);
}