如何在 Java 中绘制 RGB 色轮
How to draw a RGB Color Wheel in Java
我想在Java中绘制一个RGB色轮,但我无法通过圆形获得渐变。我只想在屏幕上绘制它,根本不需要用户交互。
这就是我现在的全部:
public void paint (Graphics g){
super.paint(g);
int red = 255;
int green = 0;
int blue = 0;
int x1 = 500;
int y1 = 305;
int x2 = 500;
int y2 = 50;
while (green != 255){
g.setColor(new Color(red, green, blue));
green++;
g.drawLine(x1, y1, x2, y2);
x2++;
if (y2 < y1){
y2++;
}
}
while (red != 0){
g.setColor(new Color(red, green, blue));
red--;
g.drawLine(x1, y1, x2, y2);
x2--;
y2++;
}
x2 = 500;
while (blue != 255){
g.setColor(new Color(red, green, blue));
blue++;
g.drawLine(x1, y1, x2, y2);
x2--;
if (y2 > y1){
y2--;
}
}
while (red != 255){
green--;
g.setColor(new Color(red, green, blue));
red++;
g.drawLine(x1, y1, x2, y2);
x2++;
y2--;
}
}
}
改变亮度
From here,似乎最简单的增亮颜色的方法是将其转换为 HSB。
float hsbVals[] = Color.RGBtoHSB( originalColor.getRed(),
originalColor.getGreen(),
originalColor.getBlue(), null );
Color highlight = Color.getHSBColor( hsbVals[0], hsbVals[1],
0.5f * ( 1f + hsbVals[2] )); // Play with this part to modify rate of change
使其循环
从你所拥有的情况来看,你基本上需要擦除中间的圆形区域以外的所有区域。
从 this answer 开始,区域 class 似乎最简单。
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g.create();
Rectangle2D rectangleNotToDrawIn = new Rectangle2D.Double(100, 100, 20, 30);
// You will need to find out what size ellipse you need
Area outside = calculateOutside(rectangleNotToDrawIn);
// draw color wheel here
g2.setPaint(Color.black); // assuming you want black
g2.setClip(outside);
g2.drawLine(0, 0, getWidth(), getHeight());
}
// Change this method to take in an ellipse shape.
private Area calculateOutside(Rectangle2D r) {
Area outside = new Area(new Rectangle2D.Double(0, 0, getWidth(), getHeight()));
outside.subtract(new Area(r));
return outside;
}
这是我在 Java 中绘制 RGB 色轮的解决方案:
public class Main {
public static void main(String[] args) {
int rad = 1024;
BufferedImage img = new BufferedImage(rad, rad, BufferedImage.TYPE_INT_RGB);
// Center Point (MIDDLE, MIDDLE)
int centerX = img.getWidth() / 2;
int centerY = img.getHeight() / 2;
int radius = (img.getWidth() / 2) * (img.getWidth() / 2);
// Red Source is (RIGHT, MIDDLE)
int redX = img.getWidth();
int redY = img.getHeight() / 2;
int redRad = img.getWidth() * img.getWidth();
// Green Source is (LEFT, MIDDLE)
int greenX = 0;
int greenY = img.getHeight() / 2;
int greenRad = img.getWidth() * img.getWidth();
// Blue Source is (MIDDLE, BOTTOM)
int blueX = img.getWidth() / 2;
int blueY = img.getHeight();
int blueRad = img.getWidth() * img.getWidth();
for (int i = 0; i < img.getWidth(); i++) {
for (int j = 0; j < img.getHeight(); j++) {
int a = i - centerX;
int b = j - centerY;
int distance = a * a + b * b;
if (distance < radius) {
int rdx = i - redX;
int rdy = j - redY;
int redDist = (rdx * rdx + rdy * rdy);
int redVal = (int) (255 - ((redDist / (float) redRad) * 256));
int gdx = i - greenX;
int gdy = j - greenY;
int greenDist = (gdx * gdx + gdy * gdy);
int greenVal = (int) (255 - ((greenDist / (float) greenRad) * 256));
int bdx = i - blueX;
int bdy = j - blueY;
int blueDist = (bdx * bdx + bdy * bdy);
int blueVal = (int) (255 - ((blueDist / (float) blueRad) * 256));
Color c = new Color(redVal, greenVal, blueVal);
float hsbVals[] = Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), null);
Color highlight = Color.getHSBColor(hsbVals[0], hsbVals[1], 1);
img.setRGB(i, j, RGBtoHEX(highlight));
} else {
img.setRGB(i, j, 0xFFFFFF);
}
}
}
try {
ImageIO.write(img, "png", new File("wheel.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static int RGBtoHEX(Color color) {
String hex = Integer.toHexString(color.getRGB() & 0xffffff);
if (hex.length() < 6) {
if (hex.length() == 5)
hex = "0" + hex;
if (hex.length() == 4)
hex = "00" + hex;
if (hex.length() == 3)
hex = "000" + hex;
}
hex = "#" + hex;
return Integer.decode(hex);
}
}
我想在Java中绘制一个RGB色轮,但我无法通过圆形获得渐变。我只想在屏幕上绘制它,根本不需要用户交互。
这就是我现在的全部:
public void paint (Graphics g){
super.paint(g);
int red = 255;
int green = 0;
int blue = 0;
int x1 = 500;
int y1 = 305;
int x2 = 500;
int y2 = 50;
while (green != 255){
g.setColor(new Color(red, green, blue));
green++;
g.drawLine(x1, y1, x2, y2);
x2++;
if (y2 < y1){
y2++;
}
}
while (red != 0){
g.setColor(new Color(red, green, blue));
red--;
g.drawLine(x1, y1, x2, y2);
x2--;
y2++;
}
x2 = 500;
while (blue != 255){
g.setColor(new Color(red, green, blue));
blue++;
g.drawLine(x1, y1, x2, y2);
x2--;
if (y2 > y1){
y2--;
}
}
while (red != 255){
green--;
g.setColor(new Color(red, green, blue));
red++;
g.drawLine(x1, y1, x2, y2);
x2++;
y2--;
}
}
}
改变亮度
From here,似乎最简单的增亮颜色的方法是将其转换为 HSB。
float hsbVals[] = Color.RGBtoHSB( originalColor.getRed(),
originalColor.getGreen(),
originalColor.getBlue(), null );
Color highlight = Color.getHSBColor( hsbVals[0], hsbVals[1],
0.5f * ( 1f + hsbVals[2] )); // Play with this part to modify rate of change
使其循环
从你所拥有的情况来看,你基本上需要擦除中间的圆形区域以外的所有区域。
从 this answer 开始,区域 class 似乎最简单。
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g.create();
Rectangle2D rectangleNotToDrawIn = new Rectangle2D.Double(100, 100, 20, 30);
// You will need to find out what size ellipse you need
Area outside = calculateOutside(rectangleNotToDrawIn);
// draw color wheel here
g2.setPaint(Color.black); // assuming you want black
g2.setClip(outside);
g2.drawLine(0, 0, getWidth(), getHeight());
}
// Change this method to take in an ellipse shape.
private Area calculateOutside(Rectangle2D r) {
Area outside = new Area(new Rectangle2D.Double(0, 0, getWidth(), getHeight()));
outside.subtract(new Area(r));
return outside;
}
这是我在 Java 中绘制 RGB 色轮的解决方案:
public class Main {
public static void main(String[] args) {
int rad = 1024;
BufferedImage img = new BufferedImage(rad, rad, BufferedImage.TYPE_INT_RGB);
// Center Point (MIDDLE, MIDDLE)
int centerX = img.getWidth() / 2;
int centerY = img.getHeight() / 2;
int radius = (img.getWidth() / 2) * (img.getWidth() / 2);
// Red Source is (RIGHT, MIDDLE)
int redX = img.getWidth();
int redY = img.getHeight() / 2;
int redRad = img.getWidth() * img.getWidth();
// Green Source is (LEFT, MIDDLE)
int greenX = 0;
int greenY = img.getHeight() / 2;
int greenRad = img.getWidth() * img.getWidth();
// Blue Source is (MIDDLE, BOTTOM)
int blueX = img.getWidth() / 2;
int blueY = img.getHeight();
int blueRad = img.getWidth() * img.getWidth();
for (int i = 0; i < img.getWidth(); i++) {
for (int j = 0; j < img.getHeight(); j++) {
int a = i - centerX;
int b = j - centerY;
int distance = a * a + b * b;
if (distance < radius) {
int rdx = i - redX;
int rdy = j - redY;
int redDist = (rdx * rdx + rdy * rdy);
int redVal = (int) (255 - ((redDist / (float) redRad) * 256));
int gdx = i - greenX;
int gdy = j - greenY;
int greenDist = (gdx * gdx + gdy * gdy);
int greenVal = (int) (255 - ((greenDist / (float) greenRad) * 256));
int bdx = i - blueX;
int bdy = j - blueY;
int blueDist = (bdx * bdx + bdy * bdy);
int blueVal = (int) (255 - ((blueDist / (float) blueRad) * 256));
Color c = new Color(redVal, greenVal, blueVal);
float hsbVals[] = Color.RGBtoHSB(c.getRed(), c.getGreen(), c.getBlue(), null);
Color highlight = Color.getHSBColor(hsbVals[0], hsbVals[1], 1);
img.setRGB(i, j, RGBtoHEX(highlight));
} else {
img.setRGB(i, j, 0xFFFFFF);
}
}
}
try {
ImageIO.write(img, "png", new File("wheel.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static int RGBtoHEX(Color color) {
String hex = Integer.toHexString(color.getRGB() & 0xffffff);
if (hex.length() < 6) {
if (hex.length() == 5)
hex = "0" + hex;
if (hex.length() == 4)
hex = "00" + hex;
if (hex.length() == 3)
hex = "000" + hex;
}
hex = "#" + hex;
return Integer.decode(hex);
}
}