在Java中将数字转换为灰度颜色 [英] Converting a number to a greyscale color in Java

查看:470
本文介绍了在Java中将数字转换为灰度颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将1和50之间的数字转换为可用于此处的灰度颜色:

I'm trying to figure out how I can convert a number between 1 and 50 to a greyscale color that could be used here:

g.setColor(MyGreyScaleColour);

1将是最轻的(白色),50将是最黑的(黑色)。

1 would be lightest (white) and 50 would be darkest (black).

例如

Color intToCol(int colNum)  
{  
code here  
}  

有任何建议吗?

推荐答案

Java使用RGB颜色,其中每个组件(红色,绿色,蓝色)的范围从0-255。当所有组件具有相同的值时,您将得到白色 - 黑色 - 灰色的颜色。更接近255的组合将更白,并且更接近0将是全黑。下面的函数将返回一个灰色的颜色,白色的数量根据输入相应地改变。

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);
}

这篇关于在Java中将数字转换为灰度颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆