Best way to represent a color in a SQL Database?

2019-02-16 21:56发布

If I am using .Net and SQL Server 2008, what is the best way for me to store a color in the database, should I use ToString or convert it to an integer, or something else?

Edit:

All I want from the colour is to be able to retrieve it and draw something on the screen in the specified colour. I don't need to be able to query against it.

6条回答
成全新的幸福
2楼-- · 2019-02-16 22:34

Well, I'd store them as six digit hex codes. That opens up the interesting possibility of actually searching for colors between other colors. If you really want to make it powerful, keep the R, G, B hex numbers in three columns. That lets you find colors which contain so much red, or ones that are similar to another color (sort by average difference of the three values).

查看更多
该账号已被封号
3楼-- · 2019-02-16 22:38

How to store information in a database depends on how you intend to use and access it. There's no saying what the best way to store it is without knowing how you'll use it.

查看更多
Root(大扎)
4楼-- · 2019-02-16 22:39

If you are storing the System.Drawing.Color, you need to store 4 bytes that represent the alpha and the 3 color channels. You can use a int data type.

If you are storing the System.Windows.Media.Color (from WPF), there are two possibilities depending on the usage you are using:

  1. If you are only using ARGB colors, store as a int data type.
  2. If you are using the ScRGB format, you need to store 4 float (single) values. I suggest two way of storing it:
    1. A user-defined type with 4 float fields.
    2. A varchar (length depend on the precision) and storing
      color.ToString(CultureInfo.InvariantCulture)
查看更多
老娘就宠你
5楼-- · 2019-02-16 22:39

Depends so much on the type of color you want to store. Eg. if it is from a fixed palette then a short int or even an unsigned byte might be sufficient.

3 byte RGB or 4 byte ARGB (A=alpha, ie. transparency) could fit into 32 bit, so a 32 bit unsigned integer type would work. This is the standard for most applications. However, you might want more - in which case a 64 bit unsigned integer might be required.

Alternatively, if you want to modify or query the color according to its components, I would store each component as its own unsigned int (ie. Red, Green, Blue, Alpha fields).

查看更多
闹够了就滚
6楼-- · 2019-02-16 22:44

How are the colors stored natively?

If you're just using 0xRRGGBB format, you may as well store it as an integer in the database, and re-hexify it when you SELECT (for readability).

查看更多
祖国的老花朵
7楼-- · 2019-02-16 22:52

Color can be surprisingly tricky on some industries like digital cameras, desktop publishing, sanners and other. Most programmers associate color with the 24 bit color (RGB usually), some associate it with the 32 bit (RGBA). The few that work in industries like DTP that make heavy use of color have a richer set of terms that includes color correction, color space and so on and so forth. So what exactly do you need to store?

  • Do you need to store a single format that is not going to change?
  • Do you need to store multiple formats and know what format is actually stored (RGB vs. RGBA vs. CMY vs. HSV etv) ? Note that even something apparently as simple as RGB actually can be Adobe RGB or sRGB.
  • Do you need to store color space and correction? Note often the RGB color has no meaning w/o proper color management.
  • Do you need to store a simple text description ('red', 'lime', 'teal' etc) ?
  • Do you need to store the 'web' color (ie. RGB or RGBA as hex) ?
查看更多
登录 后发表回答