Brighten RGB color? [solved]

I thought I’d be able to find an answer to this, but it’s turned out to be harder than I thought.

In MS paint, there’s a color editor tool that lets you brighten colors

My question is, what is the math behind that color brightening tool, rgb wise?

I thought that if I took a color, and converted the brightness in the “HSB” color model presented by the Color class, I would get an identical effect. However, this isn’t quite what I was looking for. For example, it’s impossible for me to brighten purple all the way up to white. It merely becomes a very bright purple. Using the Color class “brighten” and “darken” methods produce the same result.

So, simply put, how, numerically, can I implement a method that brightens or darkens a color by an amount that produces an effect identical to that in the picture?

Thanks

The problem here is that HSB is easy to confuse with HSV

Found this on wikipedia. Hopefully it helps show the difference.

Ahhh, I thought HSB was the same as HSL, but it is actually the same as HSV. So that makes more sense. If anyone knows of a way to convert from HSB to HSL, I’d be appreciate it.

I’ll try to find something else, but here’s the Wikipedia article if it helps.

EDIT: Here we go
EDIT 2: And this

That first article was very good! Using it, I made my own class that can brighten colors in the way I wanted.

I will post it here in case someone else in the future is trying to do the same thing.

I tested it and it works :slight_smile:

import java.awt.Color;

public class ColorOperator {
	
	//colorInt should be a standard rgb hex value, like 0xffffff
	//light  should be a number from 0 to 255, 0 being black, 255 being white
	//returns a hex rgb int at the appropriate brightness level
	public static int setColorBrightness(int colorInt, int light){
		int r = ( colorInt >> 16 ) & 0xff;
		int g = ( colorInt >> 8 ) & 0xff;
		int b = ( colorInt ) & 0xff;

		float[] hsb = Color.RGBtoHSB(r, g, b, null);
		float[] hsl = HSBtoHSL(hsb);
		hsl[2] = light / 255f;
		hsb = HSLtoHSB(hsl);
				
		return Color.HSBtoRGB(hsb[0], hsb[1], hsb[2]) & 0xffffff;
	}
	
	public static float[] HSBtoHSL(float[] hsb){
		float[] hsl = new float[3];
		
		float l = (2 - hsb[1]) * hsb[2] / 2;
		float d = (l < .5f ? l * 2 : 2f - l * 2);
		
		hsl[0] = hsb[0];
		hsl[1] = d == 0 ? 0 : hsb[1] * hsb[2] / d;
		hsl[2] = l;
		
		return hsl;
	}
	
	public static float[] HSLtoHSB(float[] hsl){
		float[] hsb = new float[3];
		
		float b = hsl[2] + hsl[1] * (hsl[2] < .5f ? hsl[2] * 2 : 2 - hsl[2] * 2) / 2;
		
		hsb[0] = hsl[0];
		hsb[1] = b == 0 ? 0 : 2 * (1 - hsl[2] / b);
		hsb[2] = b;
		
		return hsb;
	}

}