Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Turning dark color to bright in java

Tags:

java

colors

I am working on a program where I take RGB values from a portion of an image. I want to remove the darkness in the color and make it bright. What I do is I use Color.RGBtoHSB I then take the brightness channel and set it to the highest value it can be in range then convert the HSB back to RGB. However, when I do this the color changes completely. Here is an example with dark red and it turning to purple and the code I use to do this.

System.out.println("Before Conversion:");
System.out.println("R: " + rAvg  + "\nG :" + gAvg + "\nB :" + bAvg);
Color.RGBtoHSB(rAvg, gAvg, bAvg, hsv);

hsv[2] = 100; //Set to max value
System.out.println("H: " + hsv[0] * 360 + "\nS: " +  hsv[1] * 100 + "\nV :" + hsv[2]);

int rgb = Color.HSBtoRGB(hsv[0], hsv[1], hsv[2]);
System.out.println("After conversion");
Color color = new Color(rgb);
System.out.println("R: " + color.getRed());
System.out.println("G: " + color.getGreen());
System.out.println("B: " + color.getBlue());

Output:

Before Conversion:
R: 128
G :39
B :50
H: 352.58426
S: 69.53125
V :100.0
After conversion
R: 158
G: 126
B: 233
like image 333
cuber Avatar asked Oct 29 '22 09:10

cuber


1 Answers

The brightness, hsv[2], needs to be a value between 0 and 1. Try these two lines of code:

    hsv[2] = 1; //Set to max value
    System.out.println("H: " + hsv[0] * 360 + "\nS: " +  hsv[1] * 100 + "\nV :" + hsv[2] * 100);
like image 192
Phil Grigsby Avatar answered Nov 15 '22 04:11

Phil Grigsby