In regular C#, there’s a quick contrast script like this:
public void SetContrast(double contrast)
{
Bitmap temp = (Bitmap)_currentBitmap;
Bitmap bmap = (Bitmap)temp.Clone();
if (contrast < -100) contrast = -100;
if (contrast > 100) contrast = 100;
contrast = (100.0 + contrast) / 100.0;
contrast *= contrast;
Color c;
for (int i = 0; i < bmap.Width; i++)
{
for (int j = 0; j < bmap.Height; j++)
{
c = bmap.GetPixel(i, j);
double pR = c.R / 255.0;
pR -= 0.5;
pR *= contrast;
pR += 0.5;
pR *= 255;
if (pR < 0) pR = 0;
if (pR > 255) pR = 255;
double pG = c.G / 255.0;
pG -= 0.5;
pG *= contrast;
pG += 0.5;
pG *= 255;
if (pG < 0) pG = 0;
if (pG > 255) pG = 255;
double pB = c.B / 255.0;
pB -= 0.5;
pB *= contrast;
pB += 0.5;
pB *= 255;
if (pB < 0) pB = 0;
if (pB > 255) pB = 255;
bmap.SetPixel(i, j,
Color.FromArgb((byte)pR, (byte)pG, (byte)pB));
}
}
_currentBitmap = (Bitmap)bmap.Clone();
}
Is there a way to do something like this in Unity using C#? If so, what would be the Bitmap class, would it be Texture2D instead?
EDIT Unity version so far:
public void ApplyContrast(string contrast)
{
double contrastD = Convert.ToDouble(contrast);
Texture2D bitmapImage = new Texture2D(imgTexture.width, imgTexture.height);
if (contrastD < -100) contrastD = -100;
if (contrastD > 100) contrastD = 100;
contrastD = (100.0 + contrastD) / 100.0;
contrastD *= contrastD;
Color color;
for (int i = 0; i < bitmapImage.width; i++)
{
for (int j = 0; j < bitmapImage.height; j++)
{
color = bitmapImage.GetPixel(i, j);
double pR = color.r / 255.0;
pR -= 0.5;
pR *= contrastD;
pR += 0.5;
pR *= 255;
if (pR < 0) pR = 0;
if (pR > 255) pR = 255;
double pG = color.g / 255.0;
pG -= 0.5;
pG *= contrastD;
pG += 0.5;
pG *= 255;
if (pG < 0) pG = 0;
if (pG > 255) pG = 255;
double pB = color.b / 255.0;
pB -= 0.5;
pB *= contrastD;
pB += 0.5;
pB *= 255;
if (pB < 0) pB = 0;
if (pB > 255) pB = 255;
bitmapImage.SetPixel(i, j,
FromArgb((byte)pR, (byte)pG, (byte)pB));
}
}
bitmapImage.Apply();
Image.renderer.material.mainTexture = bitmapImage as Texture;
}
Where
public static Color FromArgb(int red, int green, int blue)
{
// float fa = ((float)alpha) / 255.0f;
float fr = ((float)red) / 255.0f;
float fg = ((float)green) / 255.0f;
float fb = ((float)blue) / 255.0f;
return new Color(fr, fg, fb);
}