Tech Off Thread

9 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Storing Colors in SQL database

Back to Forum: Tech Off
  • User profile image
    Shark_M

    Hi guys,
       How do i store colors in database?
    What datatype is a color?

    For example i want to store the color of an item, it has Red color or Black color?

    I want to store the Hex value or something, so i can retrive it programmatically and display the item with the color specified in the database

    what data type do i store it in? varchar (50)? or as what?

    I am using C#

    thanks

  • User profile image
    Shark_M

    I have the following scheme, that stored the Color in Hexadecimal, HTML fomat with # and 6 hex value

    like this

    HexToColor

    public static Color HexToColor(String hexString)
    //  Translates a html hexadecimal definition of a color into a .NET Framework Color.
    //  The input string must start with a '#' character and be followed by 6 hexadecimal
    //  digits. The digits A-F are not case sensitive. If the conversion was not successfull
    //  the color white will be returned.

    {
    Color actColor;
    int r,g,b;
    r=0;
    g=0;
    b=0;
    if ((hexString.StartsWith("#"))&&(hexString.Length==7))
    {
    r=HexToInt(hexString.Substring(1,2));
    g=HexToInt(hexString.Substring(3,2));
    b=HexToInt(hexString.Substring(5,2));
    actColor=Color.FromArgb(r,g,b);
    }
    else
    {
    actColor=Color.White;
    }
    return actColor;
    }
    ColorToHex

    public static String ColorToHex(Color actColor)
    //  Translates a .NET Framework Color into a string containing the html hexadecimal
    //  representation of a color. The string has a leading '#' character that is followed
    //  by 6 hexadecimal digits.
    {
    return         "#"+IntToHex(actColor.R,2)+IntToHex(actColor.G,2)+IntToHex(actColor.B,2);
    }


    Is there a better method than this that is faster than this in .net 2.0 framework?

  • User profile image
    TommyCarlier

    I think it's a lot more efficient and faster if you store it as an integer. To get the integer, call Color.ToArgb(); to get the color from an integer, call Color.FromArgb(i);

  • User profile image
    hupfis

    As TommyCarlier wrote, store the color as an Integer:

    To create the int from the hexvalue:
    int.Parse(hexvalue, System.Globalization.NumberStyles.HexNumber);

    To create the hex color from the integer:
    string hexvalue = "00000" + intColor.ToString("X");
    hexvalue = "#" + hexvalue.Substring(hexvalue.Length - 6);

    hupfis

  • User profile image
    TommyCarlier

    An easier way to create the hex color from the integer: string hexvalue = intColor.ToString("X6");

  • User profile image
    Shark_M

    TommyCarlier wrote:
    I think it's a lot more efficient and faster if you store it as an integer. To get the integer, call Color.ToArgb(); to get the color from an integer, call Color.FromArgb(i);



    How to user Color .ToArgb ()?

    forexample i wan to to store color.Red,

    is it this way?

    Color   someColor = Color.Red;

    int TempColor;
    TempColor = someColor.ToArgb();

    then i store TempColor in SQL table with int field

    then i recall it using stored procedure,
    then i submit to a functiont that uses

    color storedColor = Color.FromArgb (The Value I get from stored procedure)
    is that how i should do it?

  • User profile image
    TommyCarlier

    That's the right way to do it.

  • User profile image
    Harlequin

    1121314111
    Like this?

    How does it know if the color is:
    112   13      141    11
    11     213    141    11
    11     213    14      111
    112   13      14      111
    112   131    14      111
    etc etc etc?

  • User profile image
    TommyCarlier

    It doesn't work like that: it's a binary combination. A color is defined as 32 bits: 8 bits alpha (translucency), 8 bits red, 8 bits green and 8 bits blue.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.