-2

I allow my users to set a background colour in their app (ie #D900D9), which is stored as a string. I then would like to set a property, 'border' which will take the hex value 'background' and the darkness say by 20%?( ie #770077) How can I achieve this?

public string backgroundColor { get; set; }
 public string borderColor
        {
            get
            {
                return backgroundColor + 20%
            } 
        }
D-W
  • 5,201
  • 14
  • 47
  • 74
  • nope, I just dont where to start, lots of RGB examples, just wondering if something simple existed out there? – D-W Apr 05 '17 at 11:28

2 Answers2

2

You'd need to create a Color object

var yourColor = Color.FromHex(backgroundColor);

And then just factor the RGB values:

var c2 = Color.FromArgb(yourColor.A,
    (int)(yourColor.R * 0.8), (int)(yourColor .G * 0.8), (int)(yourColor.B * 0.8));

(which should darken it; or, for example, * 1.25 to brighten it)

Credits: How do I adjust the brightness of a color? (@Marc Gravell)

Community
  • 1
  • 1
greenhoorn
  • 1,601
  • 2
  • 15
  • 39
0
public string backgroundColor { get; set; } 
public string borderColor
        {
get
            {
                Color c1 = System.Drawing.ColorTranslator.FromHtml(backgroundColor);
                Color c2 = Color.FromArgb(c1.A,(int)(c1.R * 0.8), (int)(c1.G * 0.8), (int)(c1.B * 0.8));
                return System.Drawing.ColorTranslator.ToHtml(c2);
            } 
}
D-W
  • 5,201
  • 14
  • 47
  • 74