java 如何在HSB颜色系统中改变颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7488117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:11:16  来源:igfitidea点击:

how to change color in HSB color system

java

提问by 5YrsLaterDBA

I am studying the sample code from the last answer on this post

我正在研究这篇文章最后一个答案中的示例代码

to see how to make a good flashing button. That sample code use the following code to specify the colors:

看看如何制作一个好的闪烁按钮。该示例代码使用以下代码来指定颜色:

           for (int i = 0; i < N; i++) 
            {                 
                clut.add(Color.getHSBColor(1, 1 - (i / N), 1));             
            }             
            for (int i = 0; i < N; i++) 
            {                 
                clut.add(Color.getHSBColor(1, i / N, 1));             
            } 

Color.getHSBColor(1, 1 - (i / N), 1)is the place building the colors. The first parameter (Hue) of the getHSBColor()will specify the base color. So if I change it to 230, the colors should be bluebased colors; if it is 60, the colors should be yellowbased. But the sample program doesn't work as I expected. There is no flashing color changes after I set the Hue to different value. Anybody knows why?

Color.getHSBColor(1, 1 - (i / N), 1)是构建颜色的地方。getHSBColor()的第一个参数 (Hue )将指定基色。因此,如果我将其更改为230,则颜色应该是基于蓝色的颜色;如果是60,颜色应该是黄色的。但是示例程序并没有像我预期的那样工作。将 Hue 设置为不同的值后,没有闪烁的颜色变化。有谁知道为什么?

采纳答案by woliveirajr

Color.getHSBColor()should receive floating point numbers from 0 to 1, so any value bigger than 1 will be treated like 1...

Color.getHSBColor()应该接收从 0 到 1 的浮点数,因此任何大于 1 的值都将被视为 1...

Take a look:

看一看:

The hue parameter is a decimal number between 0.0 and 1.0 which indicates the hue of the color. You'll have to experiment with the hue number to find out what color it represents.

For example, setPenColor(Color.getHSBColor(0.56f, 1.0f, 0.8f));

色调参数是一个介于 0.0 和 1.0 之间的十进制数,表示颜色的色调。您必须对色调编号进行试验,以找出它代表什么颜色。

例如, setPenColor(Color.getHSBColor(0.56f, 1.0f, 0.8f));

source: http://www.otherwise.com/Lessons/ColorsInJava.html

来源:http: //www.otherwise.com/Lessons/ColorsInJava.html

For example, you could do:

例如,你可以这样做:

        float hue = your_color/255; // if you want to use 0-255 range

        for (int i = 0; i < N; i++) {                 
            clut.add(Color.getHSBColor(hue, 1 - (i / N), 1));             
        }             
        for (int i = 0; i < N; i++) {                 
            clut.add(Color.getHSBColor(hue, i / N, 1));             
        }