Show and Invert RGB Colors in C#

This post will demonstrate how to show and convert colors when the Red Green & Blue values of those colors are given. We simple use a function and supply the Red, Green and Blue components in the parameter list. We shall also see how to Invert a color. In the image below the text color is set by the user. The background color is automatically set which is the Invert of the Text color. See image below to have a look at the final output of our WinForms application:

The Color.FromArgb( ) function sets the color of any element by taking in 3 basic parameters. These parameters set the RGB components and create the color we need. The 'A' in Argb specifies the Alpha value which sets the brightness of the color. Although its not necessary, it is a good practice to set the Alpha value of a color. This enables you to fine tune the color even further. 

We also see how to Invert a color. Inverting a color simply means taking the opposite value of each component and merge them to create the Inverted color. To perform inversion in the RGB values, we simply minus the color from the integer 255. If we have a color component with value 255 then the inverted color component is (255 - 255) i.e. 0 (Zero). Likewise the color component with value 100 is (255 - 100) i.e. 155. The invert of Black is White as we all know.  As you can see in the above image, the invert color of blue is yellow. 

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Color_Show_by_Integer_Value
    public partial class Form1 : Form
        public Form1()
        private void btnShow_Click(object sender, EventArgs e)
            int RedValue = Convert.ToInt16(TRed.Text);
            int GreenValue = Convert.ToInt16(TGreen.Text);
            int BlueValue = Convert.ToInt16(TBlue.Text);

            LabelColor.BackColor =         Color.FromArgb(RedValue,GreenValue,BlueValue);

            LabelExplain.BackColor = 
      Color.FromArgb(255 - RedValue, 255 - GreenValue, 255 - BlueValue);

            LabelExplain.ForeColor = Color.FromArgb(RedValue, GreenValue, BlueValue);
Download Visual Studio 2012 Code Directly from Here

Custom Search