问题描述
我使用 java.awt.Color
实例。有什么办法对颜色进行算术运算吗?像 rgb(20,20,20)+ rgb(10,200,170)= rgb(30,220,190)
?
我想要做的:我有一个gui有一个表,其中如果用户点击一个单元格,其他单元格根据它们之间的关系改变颜色。我正在寻找一种方法来避免硬编码什么基本颜色,以及它们改变哪些颜色值。
所以选择的单元格可能是 rgb(255,0,0)
,其他一切可能在 rgb(0,0,0)
和 rgb(0,255,0)
。我在想...枚举?
import java.awt.Color;
public enum ColorConstant {
SELECTED(new rgb(255,0,0),Red),
MAX_DISTANCE(new rgb(0,255,0),绿色)
私人色彩;
private ??? whichColorToModify;
}
除此之外,我通常写一个小的实用程序方法用于这样的目的,如:
private static颜色亮度(Color c,double scale){
int r = Math.min(255,(int)(c.getRed * scale));
int g = Math.min(255,(int)(c.getGreen()* scale));
int b = Math.min(255,(int)(c.getBlue()* scale));
return new Color(r,g,b);
}
I'm working with java.awt.Color
instances. Is there any way to do arithmetic operations on colors? Something like rgb(20, 20, 20) + rgb(10, 200, 170) = rgb(30, 220, 190)
?
What I'm trying to do: I have a gui that features a table, where if the user clicks on a cell, the other cells change color based on their relationship to the selected one. I'm looking for a way to avoid hard coding what the base colors are, and on which color values they change.
So the selected cell might be rgb(255, 0, 0)
, and everything else might be between rgb(0, 0, 0)
and rgb(0, 255, 0)
based on their values. I'm thinking... enums?
import java.awt.Color;
public enum ColorConstant {
SELECTED (new rgb(255, 0, 0), "Red"),
MAX_DISTANCE (new rgb(0, 255, 0), "Green")
private Color shade;
private ??? whichColorToModify;
}
There are the methods Color.brighter and Color.darker.
Other than that, I usually write a small utility methods for such purposes, like:
private static Color brightness(Color c, double scale) {
int r = Math.min(255, (int) (c.getRed() * scale));
int g = Math.min(255, (int) (c.getGreen() * scale));
int b = Math.min(255, (int) (c.getBlue() * scale));
return new Color(r,g,b);
}
这篇关于Java:颜色操作(加,减)? - 常量类中的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!