Turn dark to bright in java

I am working on a program where I take RGB values ​​from part of an image. I want to remove the darkness in color and make it bright. What I do, I use Color.RGBtoHSB Then I take the brightness channel and set it to the highest value that can be in the range, and then converts the HSB back to RGB. However, when I do this, the color changes completely. Here is a dark red example and it turns purple and the code I use for 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 
+5
source share
1 answer

The brightness of hsv[2] should be a value from 0 to 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); 
+2
source

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


All Articles