- Home /
 
Default color issue
I currently have a struct named Colors. Here's its code:-
 using UnityEngine;
 
 struct Colors
 {
     public Color defBtnDisabledColor
     {
         get
         {
             return Color32(32, 32, 32, 128);
         }
     }
 }
 
               
 So, what happens is when I set any color to Colors.defBtnDisabledColor, it always set it to #1A1A1A with 26, 26, 26, 128 RGBA values. I expect it to have #202020 with 128 alpha as it is the color hex.
 if anyone knows why is it happening, what could be causing this, please let me know. Thanks in advance.
 EDIT: This issue is only occurring when the color is having alpha less than 255. I have other colors too and I am setting their values just like this only except they have 255 alpha. They are working just fine. 
Answer by Statement · May 31, 2021 at 09:24 AM
Maybe you have a different property that you are reading by mistake.
I created a unit test to verify that your code works.
 using NUnit.Framework;
 using UnityEngine;
 
 namespace Tests
 {
     struct Colors
     {
         public static Color defBtnDisabledColor
         {
             get
             {
                 return new Color32(32, 32, 32, 128);
             }
         }
     }
 
     public class ColorTests
     {
         [Test]
         public void defBtnDisabledColorShouldBe_20202080()
         {
             Color color = Colors.defBtnDisabledColor;
             string actual = ColorUtility.ToHtmlStringRGBA(color);
 
             // Passes test
             Assert.AreEqual("20202080", actual);
         }
     }
 }
 
              Answer by SadeqSoli · May 31, 2021 at 04:52 PM
Hi @HitarthPadaliya , I hope this code will help you, also your cod should work as I understand it but anyway there two other way to do it: one is Using Hex name and another one is by using color struct instead of color32 which seems alright. I did test this and it worked smoothly, let me know if you had any problem. good luck, cheers. :)
  using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 
 
 public static class _Color
 {
     #region ColorGeneral
     public static Color Red
     {
         get
         {
             Color redColor = new Color(0.8f, 0.03f, 0f, 1f);
             return redColor;
         }
     }
 public static Color BeginnerButtonColor
     {
         get
         {
             Color BpurpleColor;
             if (ColorUtility.TryParseHtmlString("#FF8ACB", out BpurpleColor))
             {
                 //return BpurpleColor;
             }
             return BpurpleColor;
         }
     }
    
     #endregion
 
     #region Favorite Colors
 
 // using HEXName is much easier.
     public static Color SetNewColor(string colorHEXName)
     {
         Color SuperDarkColor;
         if (ColorUtility.TryParseHtmlString(colorHEXName, out SuperDarkColor))
         {
         }
         return SuperDarkColor;
     }
     #endregion
 
 
 
 
 
 
 
 
 
 }//EndClasssss
 
 
              Your answer
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unity colours - how do I get the RGBA value outside of Unity? 1 Answer
Float to Byte for Color32 3 Answers
Slowly fades from opaque to alpha 2 Answers