2014-03-03 75 views
7

我想在openGL中绘制彩虹色的图例。以下是我到目前为止有:如何在Freeglut中绘制彩虹?

glBegin(GL_QUADS); 
for (int i = 0; i != legendElements; ++i) 
{ 
    GLfloat const cellColorIntensity = (GLfloat) i/(GLfloat) legendElements; 
    OpenGL::pSetHSV(cellColorIntensity*360.0f, 1.0f, 1.0f); 

    // draw the ith legend element 
    GLdouble const xLeft = xBeginRight - legendWidth; 
    GLdouble const xRight = xBeginRight; 
    GLdouble const yBottom = (GLdouble)i * legendHeight/
    (GLdouble)legendElements + legendHeight; 
    GLdouble const yTop = yBottom + legendHeight; 

    glVertex2d(xLeft, yTop); // top-left 
    glVertex2d(xRight, yTop); // top-right 
    glVertex2d(xRight, yBottom); // bottom-right 
    glVertex2d(xLeft, yBottom); // bottom-left 
} 

glEnd(); 

legendElements是离散的方格组成的“彩虹”的数量。 xLeft,xRight,yBottomyTop是组成每个平方的顶点。

在功能OpenGL::pSetHSV看起来是这样的:

void pSetHSV(float h, float s, float v) 
{ 
    // H [0, 360] S and V [0.0, 1.0]. 
    int i = (int)floor(h/60.0f) % 6; 
    float f = h/60.0f - floor(h/60.0f); 
    float p = v * (float)(1 - s); 
    float q = v * (float)(1 - s * f); 
    float t = v * (float)(1 - (1 - f) * s); 
    switch (i) 
    { 
     case 0: glColor3f(v, t, p); 
      break; 
     case 1: glColor3f(q, v, p); 
      break; 
     case 2: glColor3f(p, v, t); 
      break; 
     case 3: glColor3f(p, q, v); 
      break; 
     case 4: glColor3f(t, p, v); 
      break; 
     case 5: glColor3f(v, p, q); 
    } 
} 

我从http://forum.openframeworks.cc/t/hsv-color-setting/770

但是,该功能时,我得出这样它看起来像这样:

enter image description here

什么我想要的是红色,绿色,蓝色,靛蓝,紫罗兰色谱(所以我想直线迭代thr尽管色相。然而,这似乎并不是真正发生的事情。

我真的不明白是怎么RGB/HSV转换在pSetHSV()作品所以这是我很难找出问题..

编辑:这是固定的版本,由Jongware作为灵感(矩形正在不正确地绘制)

// draw legend elements 
glBegin(GL_QUADS); 
for (int i = 0; i != legendElements; ++i) 
{ 
    GLfloat const cellColorIntensity = (GLfloat) i/(GLfloat) legendElements; 
    OpenGL::pSetHSV(cellColorIntensity * 360.0f, 1.0f, 1.0f); 

    // draw the ith legend element 
    GLdouble const xLeft = xBeginRight - legendWidth; 
    GLdouble const xRight = xBeginRight; 
    GLdouble const yBottom = (GLdouble)i * legendHeight/
    (GLdouble)legendElements + legendHeight + yBeginBottom; 
    GLdouble const yTop = yBottom + legendHeight/legendElements; 

    glVertex2d(xLeft, yTop); // top-left 
    glVertex2d(xRight, yTop); // top-right 
    glVertex2d(xRight, yBottom); // bottom-right 
    glVertex2d(xLeft, yBottom); // bottom-left 
} 

glEnd(); 
+0

什么''legendElements''用于图片?并在一个随机的旁注:'GLdouble const yBottom =(1.0f + cellColorIntensity)* legendHeight;'' – cfrick

+0

@cfrick'legendElements'是组成“彩虹”的离散正方形的数量。 'xLeft','xRight','yBottom'和'yTop'是组成每个平方的顶点。 (将此添加到OP中)。 – arman

+1

你可以尝试每个x坐标偏移一点吗?你的颜色计算看起来很好,所以我想知道你的矩形可能是错误的尺寸,最后一个是顶部的巨大红色。 – usr2564301

回答

7

我生成光谱颜色这样的:

void spectral_color(double &r,double &g,double &b,double l) // RGB <- lambda l = < 380,780 > [nm] 
    { 
     if (l<380.0) r=  0.00; 
    else if (l<400.0) r=0.05-0.05*sin(M_PI*(l-366.0)/ 33.0); 
    else if (l<435.0) r=  0.31*sin(M_PI*(l-395.0)/ 81.0); 
    else if (l<460.0) r=  0.31*sin(M_PI*(l-412.0)/ 48.0); 
    else if (l<540.0) r=  0.00; 
    else if (l<590.0) r=  0.99*sin(M_PI*(l-540.0)/104.0); 
    else if (l<670.0) r=  1.00*sin(M_PI*(l-507.0)/182.0); 
    else if (l<730.0) r=0.32-0.32*sin(M_PI*(l-670.0)/128.0); 
    else    r=  0.00; 
     if (l<454.0) g=  0.00; 
    else if (l<617.0) g=  0.78*sin(M_PI*(l-454.0)/163.0); 
    else    g=  0.00; 
     if (l<380.0) b=  0.00; 
    else if (l<400.0) b=0.14-0.14*sin(M_PI*(l-364.0)/ 35.0); 
    else if (l<445.0) b=  0.96*sin(M_PI*(l-395.0)/104.0); 
    else if (l<510.0) b=  0.96*sin(M_PI*(l-377.0)/133.0); 
    else    b=  0.00; 
    } 
  • l是输入波长[nm] < 380,780 >
  • r,g,b被输出的RGB颜色< 0,1 >

这是简单的实际光谱颜色数据的粗略sin波近似。您也可以从中创建表并插入它或使用纹理...输出颜色如下:

spectral_colors

也有类似的不同方法:

  1. 线性颜色 - 复合梯度

  2. 人眼X,Y,Z灵敏度曲线积分

    你必须有真正精确X,Y,Z曲线,即使是轻微的偏差导致“不现实”的颜色一样,在这个例子中

    human eye XYZ sensitivity

要使最好你必须对颜色进行归一化处理,并添加指数灵敏度修正。而且这些曲线随着每一代都在变化,并且在世界的不同地区有所不同。所以除非你正在做一些特殊的医疗/物理软件,否则这不是一个好主意。

spectral colors comparison

| <- 380nm ----------------------------------------------------------------- 780nm -> |

[EDIT1]here是我的新的物理更精确的转换

我强烈建议,而不是用这种办法(更准确,更好方式)

+0

酷,我我现在在路上,但当我回家时我会检查一下。你已经把“彩虹”的概念比我的字面上的更多一点了(我真的只是想通过“色调”值进行迭代,但这很棒)。 – arman

+0

我自己发现了问题并更新了帖子。尽管如此,这仍然是一个非常好的答案,所以我会将其标记为答案。谢谢! – arman

2

那么,不完全正确。在这里我做了一个JavaScript的例子。 钠黄色(589nm下)太橙色和红色Halpha(656nm)是太黄....

保存本例中为一个HTML文件(jQuery的需要),并将其加载到浏览器: page.html中升= [纳米]

<!DOCTYPE html> 
<html><head> 
<script src='jquery.js'></script> 
<script> 


/* 
* Return parameter value of name (case sensitive !) 
*/ 
function get_value(parametername) 
{ 
    readvalue=(location.search ? location.search.substring(1) : false); 

    if (readvalue) 
    { 
     parameter=readvalue.split('&'); 
     for (i=0; i<parameter.length; i++) 
     { 
      if (parameter[i].split('=')[0] == parametername) 
      return parameter[i].split('=')[1]; 
     } 
    } 
    return false; 
} 

function spectral_color(l) // RGB <- lambda l = < 380,780 > [nm] 
{ 
    var M_PI=Math.PI; 
    var r=0,g,b; 
    if (l<380.0) r=  0.00; 
    else if (l<400.0) r=0.05-0.05*Math.sin(M_PI*(l-366.0)/ 33.0); 
    else if (l<435.0) r=  0.31*Math.sin(M_PI*(l-395.0)/ 81.0); 
    else if (l<460.0) r=  0.31*Math.sin(M_PI*(l-412.0)/ 48.0); 
    else if (l<540.0) r=  0.00; 
    else if (l<590.0) r=  0.99*Math.sin(M_PI*(l-540.0)/104.0); 
    else if (l<670.0) r=  1.00*Math.sin(M_PI*(l-507.0)/182.0); 
    else if (l<730.0) r=0.32-0.32*Math.sin(M_PI*(l-670.0)/128.0); 
    else    r=  0.00; 
     if (l<454.0) g=  0.00; 
    else if (l<617.0) g=  0.78*Math.sin(M_PI*(l-454.0)/163.0); 
    else    g=  0.00; 
     if (l<380.0) b=  0.00; 
    else if (l<400.0) b=0.14-0.14*Math.sin(M_PI*(l-364.0)/ 35.0); 
    else if (l<445.0) b=  0.96*Math.sin(M_PI*(l-395.0)/104.0); 
    else if (l<510.0) b=  0.96*Math.sin(M_PI*(l-377.0)/133.0); 
    else    b=  0.00; 
    var rgb = Math.floor(r*256)*65536+Math.floor(g*256)*256 + Math.floor(b*256); 
    rgb = '000000' + rgb.toString(16); 
    rgb = '#' + rgb.substr(-6).toUpperCase(); 

    $('#color').html([r,g,b,rgb,l]); 
    $('body').css('background-color', rgb); 
} 

</script> 
</head><body> 
<div id='color'></div> 
<script> 
spectral_color(get_value('l')); 
</script> 
</body> 
</html>