Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Brighter Color? (Java)

Tags:

java

I was trying to create a brighter color using the default colors that java.awt.Color provides

but when I draw using the 2 colors, they appear to be the same?

Color blue = Color.BLUE;
Color brighterBlue = blue.brighter();

test.setColor(blue);
test.fillCircle(size);

test.setColor(brighterBlue);
test.drawCircle(size);
like image 246
CJDKL Avatar asked Sep 06 '13 00:09

CJDKL


2 Answers

Quoting the docs:

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

So, assuming that Color.blue is the color rgb(0, 0, 255), the brighter method would attempt to:

  1. Multiply the 0s by the scale factor, resulting in 0s again; and
  2. Multiply the 255 by a scale factor, which because of capping results in 255 again.

Do note that the hue-saturation-value (where "value" is the same as "brightness") colour coordinate model is not the same as the hue-saturation-lightness model, which behaves somewhat more intuitively. (See Wikipedia on HSL and HSV.) Unfortunately Java doesn't have HSL calculations built in, but you should be able to find those by searching easily.

like image 21
millimoose Avatar answered Oct 19 '22 04:10

millimoose


There are a lot of ways to approach 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 could apply a load factor to the color instead. We know the maximum value for a color element is 255, so instead of trying to increasing the color factor itself, you could increase the color element by a factor of the over all color range, 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 255 instead...cheeky. Now we also need to take into consideration the fact that the color should never be greater than 255

This will allow you to force colors to approach white as the brighten and black as the darken...

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);

    }

}
like image 147
MadProgrammer Avatar answered Oct 19 '22 02:10

MadProgrammer