2012-07-17 51 views
0

我正在设计一个图表,它将有多个散点图。数据每个集合的散点图数量会发生变化。我试图用颜色区分散点图,但是我遇到了一些麻烦。核心情节:随机颜色的散点图

目前,我有一个for循环,为数组中的每个对象创建一个散点图。在for循环中,我设置了一个基于随机数的颜色:

lineStyle.lineColor = [CPTColor colorWithComponentRed :((arc4random()%255)/255.0)green:((arc4random()%255)/255.0 )blue:((arc4random()%255)/255.0)alpha:1.0];

这有时会起作用,但是颜色有时可能太难与其他颜色区分,或者可能完全是白色。有没有更好的方法来生成随机颜色(也许类似于饼图生成颜色的方式)?

回答

1

我不认为在这个问题中确实有任何特定的核心情节,这实际上只是一个以编程方式生成配色方案的问题。

作为一个想法如何做到这一点优于只是纯粹的随机数,继承人为我会怎么做一些几乎伪代码:

float red = 0; 
float blue = 0; 
float green = 0; 
while(need more colors){ 
    float colorToInc = (arc4Random()%100)/100; 
    float incValue = (arc4Random()%100)/500;//value between 0 and .2 
    if(colorToInc < .3){ 
     red += incValue; 
     if(red > 1) 
      red -= 1; 
    }else if(colorToInc < .7){ 
     green += incValue; 
     if(green > 1) 
      green -= 1; 
    }else{ 
     blue += incValue; 
     if(blue > 1) 
      blue -= 1; 
    } 
    newcolor = [color with red:red blue:blue green:green]; 
} 
0

对我来说,以下工作:

red = (arc4random()%100)/100.0; 
green = (arc4random()%100)/100.0; 
blue = (arc4random()%100)/100.0; 
[UIColor colorWithRed:red green:green blue:blue alpha:1];