Reputation: 36299
I am try to apply contrast to an image but I can not find a good formula to do it. I have tried many different sites formulas, and none have worked. Anyone have any suggestions?
Here is my latest attempt:
int r = Colors.red(pixel);
int g = Colors.green(pixel);
int b = Colors.blue(pixel);
float red = r / 255.0f;
float green = g / 255.0f;
float blue = b / 255.0f;
red = (((red - 0.5f) * amount) + 0.5f) * 255.0f;
green = (((green - 0.5f) * amount) + 0.5f) * 255.0f;
blue = (((blue - 0.5f) * amount) + 0.5f) * 255.0f;
int iR = (int)red;
iR = iR > 255 ? 255 : iR;
iR = iR < 0 ? 0 : iR;
int iG = (int)green;
iG = iG > 255 ? 255 : iG;
iG = iG < 0 ? 0 : iG;
int iB = (int)blue;
iB = iB > 255 ? 255 : iB;
iB = iB < 0 ? 0 : iB;
That didn't work. Anyone have any other suggestions?
Upvotes: 1
Views: 1926
Reputation: 36299
I figured it out! Here is the full code for anyone looking for the same effect.
float value = (255.0f + amount) / 255.0f;
value *= value;
for(int i = 0; i < ImageSync.previewPixels.length; i++){
int pixel = ImageSync.previewPixels[i];
int r = Colors.red(pixel);
int g = Colors.green(pixel);
int b = Colors.blue(pixel);
float red = r / 255.0f;
float green = g / 255.0f;
float blue = b / 255.0f;
red = (((red - 0.5f) * value) + 0.5f) * 255.0f;
green = (((green - 0.5f) * value) + 0.5f) * 255.0f;
blue = (((blue - 0.5f) * value) + 0.5f) * 255.0f;
int iR = (int)red;
iR = iR > 255 ? 255 : iR;
iR = iR < 0 ? 0 : iR;
int iG = (int)green;
iG = iG > 255 ? 255 : iG;
iG = iG < 0 ? 0 : iG;
int iB = (int)blue;
iB = iB > 255 ? 255 : iB;
iB = iB < 0 ? 0 : iB;
ImageSync.previewPixels[i] = Colors.rgba(iR, iG, iB);
}
Upvotes: 1