Silverlight: Colors in Hexadecimal values

 

[PT]

Gostaria de deixar um pequeno truque para se trabalhar com cores nos controlos de Siverlight. Imaginem que instanciam um controlo como por exemplo uma textbox:

var newTextBox = new TextBox();

Agora, gostaria de mudar a color de fundo para preto, vermelho, verde, etc…

Então será necessário escrever algo como: myTextBox.Background = new Brush(Opacity bla blabla, etc); etc, e mais bla… Mas eu só queria algo como .Background = Colors.Red;

Ok, eis algo que pode ajudar e muito. Criem um método para extraír de um valor Hexadecimal a vossa cor como por ex:

private SolidColorBrush GetColorFromHex(string myColor)
{
    return new SolidColorBrush(
        Color.FromArgb(
            Convert.ToByte(myColor.Substring(1, 2), 16),
            Convert.ToByte(myColor.Substring(3, 2), 16),
            Convert.ToByte(myColor.Substring(5, 2), 16),
            Convert.ToByte(myColor.Substring(7, 2), 16)
        )
    );
}

Agora, é mais simples atribuir ao nosso controlo:

newTextBox.Background = GetColorFromHex("#00FFFFFF");

Cool! não?

[EN]

Just a tip, if you want in your code work with an color in Hexadecimal format maybe a little complicate.

Imagine that you create a new control such as:

var newTextBox = new TextBox();

Now you want give a color to it, right? So to do something like myTextBox.Background = new Brush(Opacity … etc, etc, etc). :s I just want a simple red, white… or something similar…

Here’s a tip, create a method to extract a color from an Hexadecimal value:

private SolidColorBrush GetColorFromHex(string myColor)
      {
          return new SolidColorBrush(
              Color.FromArgb(
                  Convert.ToByte(myColor.Substring(1, 2), 16),
                  Convert.ToByte(myColor.Substring(3, 2), 16),
                  Convert.ToByte(myColor.Substring(5, 2), 16),
                  Convert.ToByte(myColor.Substring(7, 2), 16)
              )
          );
      }

now it’s more simple to use on your control:

newTextBox.Background = GetColorFromHex("#00FFFFFF");

Cool! Isn’t it?

autor: goncalo.chaves

Tags: , , ,

Leave a Reply

ERROR: si-captcha.php plugin says GD image support not detected in PHP!

Contact your web host and ask them why GD image support is not enabled for PHP.

ERROR: si-captcha.php plugin says imagepng function not detected in PHP!

Contact your web host and ask them why imagepng function is not enabled for PHP.


Better Tag Cloud