Creating a brighter color? (Java)

I tried to create a bright color using the default colors that java.awt.Color provides

but when I paint using 2 colors do they seem the same?

 Color blue = Color.BLUE; Color brighterBlue = blue.brighter(); test.setColor(blue); test.fillCircle(size); test.setColor(brighterBlue); test.drawCircle(size); 
+4
source share
2 answers

There are many ways to solve this problem.

The most obvious way is to add a fraction to an existing color value, for example ...

 int red = 128; float fraction = 0.25f; // brighten by 25% red = red + (red * float); 

Instead, you can apply a load factor to the color instead. We know that the maximum value for a color element is 255, so instead of trying to increase the color coefficient itself, you can increase the color element by a factor over the entire range of colors, for example

 int red = 128; float fraction = 0.25f; // brighten by 25% red = red + (255 * fraction); // 191.75 

This basically increases the color element by a factor of 255 instead of ... sassy. Now we also need to take into account the fact that color should never exceed 255

This will allow you to make colors come closer to white, as bright and black, as dark ...

enter image description here

 import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class BrighterColor { public static void main(String[] args) { new BrighterColor(); } public BrighterColor() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { } JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new TestPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class TestPane extends JPanel { public TestPane() { } @Override public Dimension getPreferredSize() { return new Dimension(200, 200); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); Rectangle rec = new Rectangle(0, 0, getWidth(), getHeight() / 2); g2d.setColor(Color.BLUE); g2d.fill(rec); g2d.translate(0, getHeight() / 2); g2d.setColor(brighten(Color.BLUE, 0.25)); g2d.fill(rec); g2d.dispose(); } } /** * Make a color brighten. * * @param color Color to make brighten. * @param fraction Darkness fraction. * @return Lighter color. */ public static Color brighten(Color color, double fraction) { int red = (int) Math.round(Math.min(255, color.getRed() + 255 * fraction)); int green = (int) Math.round(Math.min(255, color.getGreen() + 255 * fraction)); int blue = (int) Math.round(Math.min(255, color.getBlue() + 255 * fraction)); int alpha = color.getAlpha(); return new Color(red, green, blue, alpha); } } 
+7
source

Quote docs :

This method applies an arbitrary scale factor for each of the three RGB components of this color to create a brighter version of this color.

So, assuming Color.blue is the rgb(0, 0, 255) color rgb(0, 0, 255) , a brighter method will try:

  • Multiply 0 by the scaling factor, resulting in 0s; and
  • Multiply 255 by the scale factor, which is due to the fact that capping returns the result in 255 again.

Note that the color saturation value (where the “value” matches the “brightness” model) does not match the saturation brightness model , which behaves somewhat more intuitively. (See Wikipedia on HSL and HSV .) Unfortunately, Java does not have built-in HSL calculations, but you can find them.

+6
source

Source: https://habr.com/ru/post/1500835/


All Articles