I need to randomly generate colors in Swing GUI, but the problem is that I want them to be only bright.
Asked
Active
Viewed 1,970 times
2
-
I think this post may help you. http://stackoverflow.com/questions/596216/formula-to-determine-brightness-of-rgb-color – Gagan Chouhan Apr 16 '16 at 15:13
-
Some [examples](http://stackoverflow.com/search?tab=votes&q=user%3a230513%20Color.getHSBColor). – trashgod Apr 16 '16 at 16:28
1 Answers
6
Use the Color class static method getHSBColor(...)
, and make sure that the third parameter, the one that represents brightness is adequately high for you, perhaps > 0.8f (but < 1.0f).
For example, the code below uses the above method to find a random bright color:
float h = random.nextFloat();
float s = random.nextFloat();
float b = MIN_BRIGHTNESS + ((1f - MIN_BRIGHTNESS) * random.nextFloat());
Color c = Color.getHSBColor(h, s, b);
Using a Random variable called random, and a MIN_BRIGHTNESS value of 0.8f:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.*;
public class RandomBrightColors extends JPanel {
private static final int PREF_W = 500;
private static final int PREF_H = PREF_W;
private static final int RECT_W = 30;
private static final int RECT_H = RECT_W;
private static final float MIN_BRIGHTNESS = 0.8f;
private Random random = new Random();
public RandomBrightColors() {
setBackground(Color.BLACK);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < 100; i++) {
g.setColor(createRandomBrightColor());
int x = random.nextInt(getWidth() - RECT_W);
int y = random.nextInt(getHeight() - RECT_H);
g.fillRect(x, y, RECT_W, RECT_H);
}
}
private Color createRandomBrightColor() {
float h = random.nextFloat();
float s = random.nextFloat();
float b = MIN_BRIGHTNESS + ((1f - MIN_BRIGHTNESS) * random.nextFloat());
Color c = Color.getHSBColor(h, s, b);
return c;
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private static void createAndShowGui() {
RandomBrightColors mainPanel = new RandomBrightColors();
JFrame frame = new JFrame("RandomBrightColors");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
createAndShowGui();
});
}
}
edit: or if you want the colors fully saturated, change the saturation parameter to 1f:
private Color createRandomBrightColor() {
float h = random.nextFloat();
float s = 1f;
float b = MIN_BRIGHTNESS + ((1f - MIN_BRIGHTNESS) * random.nextFloat());
Color c = Color.getHSBColor(h, s, b);
return c;
}
Also note that this can be done using a 3 int parameter RGB color, but if you do this, note that one parameter should be close to but not exceeding 255, one should be close to but not below 0, and the other can be random anything between 0 and 255.

Hovercraft Full Of Eels
- 283,665
- 25
- 256
- 373