Re: Saving Colors to a db

Peter Osucha <[email protected]> Thu, 5 Jul 2007 15:26:27 -0400
Newsgroups gmane.comp.windows.devel.dotnet.winforms
Message-ID <[email protected]>
Dino,

Thanks for the code.  With the holiday here in the US, I haven't had
time to implement it yet.  I'll try to get around to it tonight.  Looks
promising.

Peter

-----Original Message-----
From: Discussion forum for developers using Windows Forms to build apps
and controls [mailto:[email protected]] On Behalf Of
Dean Cleaver
Sent: Tuesday, July 03, 2007 4:51 PM
To: [email protected]
Subject: Re: [DOTNET-WINFORMS] Saving Colors to a db

Looking through my code, I set the name into the business layer:

this.dataItem.BannerBackground = colorDialog.Color.Name;

I'm presuming this name is either "White" or "FF23DC" etc. When I write
to the data access layer I simply store that name:

base.bannerBackground = value;

So that side's pretty easy. Reading it out, I have some changes.
"base.BannerBackground" is the string as read from the db:

if (Color.FromName(base.BannerBackground.Trim()).IsKnownColor)
    return base.BannerBackground.Trim(); else {
    if (base.BannerBackground.Length > 6)
        return
base.BannerBackground.Substring(base.BannerBackground.Length - 6, 6);

    return base.BannerBackground;
}

Basically, if it's a known colour, return that name, otherwise return
the 6 char RGB hex. Then, on the business side I return just that value:

return this.programUserTypeData.BannerBackground;

And in the UI, I use another function:

colorDialog.Color = ColorFromString(this.dataItem.BannerBackground);


Here's the ColorFromString function:

public static System.Drawing.Color ColorFromString(string color)
{
    if (Color.FromName(color.Trim()).IsKnownColor)
        return Color.FromName(color.Trim());
    else
    {
        if (color.Length == 6)
            return Color.FromArgb(int.Parse(color.Substring(0, 2),
NumberStyles.HexNumber),
                int.Parse(color.Substring(2, 2),
NumberStyles.HexNumber),
                int.Parse(color.Substring(4, 2),
NumberStyles.HexNumber));
        else if(color.Length == 8)
            return Color.FromArgb(int.Parse(color.Substring(0, 2),
NumberStyles.HexNumber),
                int.Parse(color.Substring(2, 2),
NumberStyles.HexNumber),
                int.Parse(color.Substring(4, 2),
NumberStyles.HexNumber),
                int.Parse(color.Substring(6, 2),
NumberStyles.HexNumber));
        else
            throw new ApplicationException("Invalid Color String :" +
color); 
    }
}

HTH

Dino

-----Original Message-----
From: Discussion forum for developers using Windows Forms to build apps
and controls [mailto:[email protected]] On Behalf Of
Dean Cleaver
Sent: Wednesday, 4 July 2007 08:32
To: [email protected]
Subject: Re: [DOTNET-WINFORMS] Saving Colors to a db

Peter,

I've been doing this for about 6 years, so I'm confident I have the code
that works - somewhere. Let me did around for you - but basically, the
essential bits are to check the incoming string for Color.KnownColor -
i.e. is it "White" or "Red" etc. If that's not the case, check if it's 6
or 8 characters - ARGB hex or RGB hex - it changed at one point I think.

I'll post the sample code once I get a chance to look right through the
layers and see what I do with colour properties.

Dino 

-----Original Message-----
From: Discussion forum for developers using Windows Forms to build apps
and controls [mailto:[email protected]] On Behalf Of
Peter Osucha
Sent: Tuesday, 3 July 2007 07:35
To: [email protected]
Subject: [DOTNET-WINFORMS] Saving Colors to a db

Hi,

I am saving Color values to a database as integers after converting the
color using the ToArgb() method.  When I retrieve the color, I am
resetting it using the FromArgb() method.  However, this doesn't
preserve the color name if the color has a name - as shown in this
simple test proc.

        private void Button3_Click ( object sender, EventArgs e )
        {
            Color col1 = Color.DarkBlue;
            int col1int = col1.ToArgb ( );
            Color col2 = Color.FromArgb ( col1int );
            Console.WriteLine ( "{0}, {1}", col1, col2 );
        }

If I desire to save and retrieve Colors to a database and to preserve
the color name if it exists, I imagine I need to save a string of the
name instead of the argb value?

Peter