4

1から50までの数値をここで使用できるグレースケールカラーに変換する方法を理解しようとしています。

g.setColor(MyGreyScaleColour);

1が最も明るく(白)、50が最も暗くなります(黒)。

例えば

Color intToCol(int colNum)  
{  
code here  
}  

助言がありますか?

4

2 に答える 2

11

Java uses RGB colors where each component (Red, Green, Blue) ranges from 0-255. When all components have the same value, you end up with a white-black-gray color. Combinations closer to 255 would be more white and closer to 0 would be all black. The function below would return a grayish color, with the amount of white scaled accordingly with the input.

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 に答える