• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Eowyn27 · Mar 12, 2015 at 03:16 PM · contrast

Applying contrast makes the image black

Here's my contrast script:

 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 = (imgTexture as Texture2D).GetPixel(i, j);
 
                     //Alpha 
                     double pA = color.a / 255.0;
 
                     //Red 
                     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;
 
                     //Green
                     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;
 
                     //Blue
                     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, new Color((float)pR,(float)pG,(float)pB,(float)pA));
                 }
             }
 
             bitmapImage.Apply();
 
             Image.renderer.material.mainTexture = bitmapImage as Texture;
     }

I noticed if there is no contrast on the image, the image is loaded correctly but as soon as I apply some contrast starting from 1 to some value, the image turns black. What's wrong with the contrast script above?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by revolute · Mar 12, 2015 at 03:49 PM

RGB values in Color is between 0 and 1. Your code

 double pR = color.r / 255.0;
                      pR -= 0.5;
                      pR *= contrastD;
                      pR += 0.5;
                      pR *= 255;


makes it go below 0.

If color.r value is at its highest, color.r == 1. Even at with this value, 1/255 - 0.5 will yield negative values that following code cannot set back to positive values.

Just dont divide by 255 and you will have a not-black texture.

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image revolute · Mar 12, 2015 at 03:50 PM 0
Share

And dont multiply it by 255 at the end or it will be all white.

avatar image Eowyn27 · Mar 12, 2015 at 03:57 PM 0
Share

Okay, I'll give it a try. Also I want to double check and say, that I would like to restrict changing my contrast values from 0% to 100%. No negative values or above 100. Thanks.

EDIT: So it works similarly to how I had it before but the contrast algo, this one, isn't very good. I wonder why it doesn't look like some of the more professional algos for contrast.

avatar image Eowyn27 · Mar 14, 2015 at 01:34 AM 0
Share

@revolute, the same thing is happening in my brightness script! http://answers.unity3d.com/questions/923493/brightness-script-makes-image-completely-black-and.html

avatar image Eowyn27 · Mar 14, 2015 at 06:23 PM 0
Share

@revolute, I've been comparing the brightness and contrast script and it seems like the scaling for both are off. The brightness script looks fine but the contrast script looks very "cartoony". I changed my contrast script to include a corrector factor: double mappedContrast = (2*contrastD) - 100; where the script above is for contrast of -100 to 100 and the string that gets passed is from 0 to 100.

avatar image revolute · Mar 15, 2015 at 03:26 PM 0
Share

Sorry but I dont think I can help you there. I can help you make the code work but not make the code get results you wish to get.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Changing contrast of individual sprite 1 Answer

How can I change the contrast for an uploaded image? 1 Answer

How can I use this contrast script in Unity? 1 Answer

How do I default the lighting in my scene to default image "brightness"? 0 Answers

[HELP] Brightness/Contrast Slider In Unity 5? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges