@@ -20,28 +20,35 @@ public static PImage apply(PImage image, float intensity) {
2020 PImage output = image .copy ();
2121
2222 int contrastIntensity = (int ) map (intensity , -1.0f , 1.0f , -255 , 255 );
23-
24- float contrastCorrectionFactor =
25- (259.f * (contrastIntensity + 255 ))
26- /
27- (255f * (259 - contrastIntensity ));
23+ float contrastCorrectionFactor = getContrasCorrectionFactor (contrastIntensity );
2824
2925 for (int x =0 ; x < image .width ; x ++) {
3026 for (int y =0 ; y < image .height ; y ++) {
3127
3228 int [] rgb = Tools .getColors (image , x , y );
33-
34- rgb [0 ] = (int )constrain (contrastCorrectionFactor * (rgb [0 ] - 128 ) + 128 , 0 , 255 );
35- rgb [1 ] = (int )constrain (contrastCorrectionFactor * (rgb [1 ] - 128 ) + 128 , 0 , 255 );
36- rgb [2 ] = (int )constrain (contrastCorrectionFactor * (rgb [2 ] - 128 ) + 128 , 0 , 255 );
37-
29+ contrastPixel (rgb , contrastCorrectionFactor );
3830 Tools .set (output , x , y , rgb [0 ], rgb [1 ], rgb [2 ]);
3931 }
4032 }
41-
33+
4234 return output ;
4335 }
4436
37+ /**
38+ *
39+ * @param contrastIntensity value between -255 and 255
40+ * @return
41+ */
42+ static float getContrasCorrectionFactor (int contrastIntensity ) {
43+ return (259.f * (contrastIntensity + 255 ))
44+ /
45+ (255f * (259 - contrastIntensity ));
46+ }
47+
48+ static void contrastPixel (int [] rgb , float contrastCorrectionFactor ) {
49+ rgb [0 ] = (int )constrain (contrastCorrectionFactor * (rgb [0 ] - 128 ) + 128 , 0 , 255 );
50+ rgb [1 ] = (int )constrain (contrastCorrectionFactor * (rgb [1 ] - 128 ) + 128 , 0 , 255 );
51+ rgb [2 ] = (int )constrain (contrastCorrectionFactor * (rgb [2 ] - 128 ) + 128 , 0 , 255 );
52+ }
4553
46-
4754}
0 commit comments