tags:

views:

939

answers:

9

I have to store colors in database.

How could I store a color in a best manner in the database field?, by color name or something else??

+8  A: 

Probably the colour value would be best, e.g. #FFFFFF or #FF0000

Jimmeh
Or just FFFFFF or FF0000, the # is kind of superfluous in the db as if you're storing it in this form all the color strings will start with # meaning your storing needless data.
BenAlabaster
A: 

I'd go for hexadecimal notation if the colors are limited to web colors.

So for example #0000FF for blue.

More info here: http://en.wikipedia.org/wiki/Web_colors

Gerrie Schenck
+4  A: 

If its for a HTML Page, storing the #RRGGBB tag as a string is probably enough.

If its for .NET , it supports building a color from its ARGB Value

System.Drawing.Color c = System.Drawing.Color.FromArgb(int);

int x = c.ToArgb();

so you could just store that int.

Eoin Campbell
+4  A: 

Store a colour as a 24 or 32 bit integer, like in HTML/CSS i.e. #FF00CC but converted to an integer not a string.

Integers will take up less space then strings (especially VCHARs).

rjstelling
Space is so cheap these days I'd be inclined to stick with the human-readable but slightly larger way.
ceejayoz
Also, use CHAR(7), not VARCHAR, since the size of color string is always 7 characters (#RRGGBB).
SeasonedCoder
Why not store it as hex and ditch the #? The hex itself is the same as the human readable form and they all need to start with # anyway, so just prepend it in the software...
BenAlabaster
Well, having a "human readable" (how looks #bb5542 ?) color in a database does really justify any waste, even if the difference is neglictible.
MatthieuP
+3  A: 

Store it as an int

Use ToArgb and FromArgb to set and get the values.

geoff
A: 

Why don't you use both? Table structure would be Int ARGB for the Key and a varchar for the Name.

ARGB (Key), Name
FFFFFFFF  ,Black
FF000000  ,White
MrTelly
...because you open the door for inconsistent values (black as white, white as black).
Ed Guiness
Tell that to a politician or a zebra
MrTelly
+1  A: 

I suggest having a 3 column color lookup table:

ID int; Name varchar(40) null; ColorVal char(8) or int (depending on how you're representing colors)

For unnamed colors just leave the name field null

C. Ross
A: 

What format are you looking to store the colors in? CMTK, RGB, Pantone? It kinda helps to know... the strictly #RGB hex format works great if its for web colors or an application but not so good if you're trying to mix paints.

SomeMiscGuy
+2  A: 

I think it depends. If you just need to store the color, then hex notation should be fine. If you need to perform queries against specific color channels, then you'd want smallint fields for each color channel (be it RGB, ARGB, CYMK, etc).

So, for simple storage, keep it simple. If you need to perform analysis, you'll need to consider alternate options as dictated by your problem domain.

Ryan Emerle