Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (526)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (593)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Brighten RGB color? [solved]  (Read 657 times)
0 Members and 1 Guest are viewing this topic.
Offline nhmllr

Senior Devvie


Medals: 1
Projects: 3


slow and steady...


« Posted 2014-01-02 04:12:03 »

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
Offline HeroesGraveDev

JGO Kernel


Medals: 300
Projects: 11
Exp: 3 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #1 - Posted 2014-01-02 04:48:53 »

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

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


Offline nhmllr

Senior Devvie


Medals: 1
Projects: 3


slow and steady...


« Reply #2 - Posted 2014-01-02 06:06:56 »

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline HeroesGraveDev

JGO Kernel


Medals: 300
Projects: 11
Exp: 3 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #3 - Posted 2014-01-02 06:13:35 »

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

EDIT: Here we go
EDIT 2: And this

Offline nhmllr

Senior Devvie


Medals: 1
Projects: 3


slow and steady...


« Reply #4 - Posted 2014-01-03 08:10:29 »

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 Smiley

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
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;
   }

}
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

toopeicgaming1999 (72 views)
2014-11-26 15:22:04

toopeicgaming1999 (61 views)
2014-11-26 15:20:36

toopeicgaming1999 (15 views)
2014-11-26 15:20:08

SHC (29 views)
2014-11-25 12:00:59

SHC (27 views)
2014-11-25 11:53:45

Norakomi (32 views)
2014-11-25 11:26:43

Gibbo3771 (27 views)
2014-11-24 19:59:16

trollwarrior1 (40 views)
2014-11-22 12:13:56

xFryIx (78 views)
2014-11-13 12:34:49

digdugdiggy (56 views)
2014-11-12 21:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!