Click here to Skip to main content
15,313,682 members
Articles / Desktop Programming / Windows Forms
Article
Posted 16 Jan 2006

Stats

48.6K views
548 downloads
12 bookmarked

Hexadecimal Color Mixer

Rate me:
Please Sign up or sign in to vote.
2.95/5 (8 votes)
16 Jan 20062 min read
This shows how to make a control to mix a color and then export it to the HTML hexadecimal format.

The color mixer in action in my HTML Editor program!

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 stores the individual 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 fulfill the same purpose (it is still employed in my HTML editor program). The dialog window was the same, except it had a separate 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 people can use it in their 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.

C#
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 its 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.

History

  • 12/01/06: First submitted code to The Code Project.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

James Gupta
CEO Synap
United Kingdom United Kingdom
Founder & CEO of Synap, an online education platform that uses machine learning to help people learn more in less time.

Software developer, main languages currently are Objective-C, MySQL and Javascript, though I got started on C++, C# and PHP.

Comments and Discussions

 
GeneralOr... Pin
Marty Sarosi10-Jan-07 6:39
MemberMarty Sarosi10-Jan-07 6:39 
GeneralRe: Or... Pin
Mohammad Ali Azam16-May-08 12:07
MemberMohammad Ali Azam16-May-08 12:07 
GeneralSimpler convert ;-) Pin
Pop Catalin16-Jan-06 6:15
MemberPop Catalin16-Jan-06 6:15 
GeneralRe: Simpler convert ;-) Pin
James Gupta16-Jan-06 9:24
professionalJames Gupta16-Jan-06 9:24 
GeneralRe: Simpler convert ;-) Pin
Pop Catalin16-Jan-06 21:39
MemberPop Catalin16-Jan-06 21:39 
GeneralRe: Simpler convert ;-) Pin
James Gupta17-Jan-06 5:26
professionalJames Gupta17-Jan-06 5:26 
GeneralRe: Simpler convert ;-) Pin
Pop Catalin17-Jan-06 5:30
MemberPop Catalin17-Jan-06 5:30 
you must be kidding !!! "makes it more complicated than it needs to be"
what can be complicated for somethis as basic as this. and btw why is your function simpler?
for someone who doesn't know what it's supposed to do it takes a while to guess with all of those code branchings
GeneralRe: Simpler convert ;-) Pin
Pop Catalin17-Jan-06 6:10
MemberPop Catalin17-Jan-06 6:10 
GeneralRe: Simpler convert ;-) Pin
James Gupta17-Jan-06 13:24
professionalJames Gupta17-Jan-06 13:24 
GeneralRe: Simpler convert ;-) Pin
robsmith19-Jun-06 23:37
Memberrobsmith19-Jun-06 23:37 
GeneralRe: Simpler convert ;-) Pin
Pop Catalin20-Jun-06 0:24
MemberPop Catalin20-Jun-06 0:24 
GeneralRe: Simpler convert ;-) [modified] Pin
Allen G13-Jul-06 0:30
MemberAllen G13-Jul-06 0:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.