4

我试图弄清楚如何将 1 到 50 之间的数字转换为可以在此处使用的灰度颜色:

g.setColor(MyGreyScaleColour);

1 最亮(白色),50 最暗(黑色)。

例如

Color intToCol(int colNum)  
{  
code here  
}  

有什么建议么?

4

2 回答 2

11

Java 使用 RGB 颜色,其中每个分量(红、绿、蓝)的范围为 0-255。当所有组件具有相同的值时,您最终会得到白色-黑色-灰色。接近 255 的组合会更白,接近 0 的组合会全黑。下面的函数将返回浅灰色,白色的数量会随着输入而相应缩放。

Color intToCol(int colNum)
{
  int rgbNum = 255 - (int) ((colNum/50.0)*255.0);
  return new Color (rgbNum,rgbNum,rgbNum);
}
于 2010-05-06T11:16:12.897 回答
8

就像是:

float grey = (50 - colNum) / 49f;
return new Color(grey, grey, grey);
于 2010-05-06T11:09:17.357 回答