Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 6,493 times

    Color Mixer - Article

    James Gupta

    Article

    Title:       Hexadecimal Color Mixer
    Author: James Gupta
    Email: xpyder@magclan.cwhnetworks.com
    Environment: VC# 8.0 Express, XP Home Ed, Win95/98
    Keywords: Control, Hexadecimal, Color
    Level: Beginner"
    Description: This shows how to make a control to mix a color
    and then export it the HTML Hexadecimal format
    Section C# Windows Forms
    SubSection C# Controls

    Introduction

    The Color Mixer control allows users to "Mix" their own color. To do this there is a slider for each color component (R, G, and B). It then converts this to the Hexadecimal format for us in HTML applications, as well as storing the induvidual RGB bytes for later use.
    As the color is mixed, the back-color of the control changes to match the current color to give the user an indication of the color they have created.

    Background

    This started off as a dialog window to fufill the same purpose (it is still employed in my HTML editor program). The dialog window was the same, except it had a seperate preview panel and textboxes to display the R, G, B and RGB values as well as the Hexadecimal string.
    It was made by myself to provide a way for my users to create HTML-Compliant color codes in a visual environment.
    To get this done, I had to create a function to convert RGB codes to the Hexadecimal number-system.

    Using the code

    I am releasing the ColorMixer.dll under the GNU license, so all people can use it in applications, but please retain copyright notices and give me credit where it is due. I would not like people to set up another tutorial using my code, however.

    The code is fairly simple in itself, and it gives you a multitude of ways to use it (pop-up, docked toolbar etc). I will explain sections of code which I found particularly challenging to write or pointers on how to improve / modify this sample.

    Points of Interest

    The control in itself was easy enough to write, but one method deserves particular attention, the RGBtoHEX() method. As the name suggests, this converts an RGB color value to a Hexadecimal value so that it can be used in HTML code etc.

          public string RGBtoHEX(int Value)
    {
    int Result = (Value / 16);
    int Remain = (Value % 16);

    string Resultant = null;

    if (Result >= 10)
    {
    if (Result == 10)
    Resultant = "A";
    if (Result == 11)
    Resultant = "B";
    if (Result == 12)
    Resultant = "C";
    if (Result == 13)
    Resultant = "D";
    if (Result == 14)
    Resultant = "E";
    if (Result == 15)
    Resultant = "F";
    }

    else Resultant = Result.ToString();

    if (Remain >= 10)
    {
    if (Remain == 10)
    Resultant += "A";
    if (Remain == 11)
    Resultant += "B";
    if (Remain == 12)
    Resultant += "C";
    if (Remain == 13)
    Resultant += "D";
    if (Remain == 14)
    Resultant += "E";
    if (Remain == 15)
    Resultant += "F";
    }

    else Resultant += Remain.ToString();

    return Resultant;
    }

    This could be changed using an enumeration of hexadecimal values, but the current system fits it's purpose well enough.

    Contact

    Please send all emails to xpyder@magclan.cwhnetworks.com
    I do have MSN Messenger, my email address for this is jamespraveen@aol.com

    Comments