Best way to represent a color in a SQL Database?

前端 未结 6 1146
情歌与酒
情歌与酒 2021-01-12 04:14

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?

相关标签:
6条回答
  • 2021-01-12 04:18

    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)
    0 讨论(0)
  • 2021-01-12 04:21

    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).

    0 讨论(0)
  • 2021-01-12 04:25

    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) ?
    0 讨论(0)
  • 2021-01-12 04:33

    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).

    0 讨论(0)
  • 2021-01-12 04:36

    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).

    0 讨论(0)
  • 2021-01-12 04:45

    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.

    0 讨论(0)
提交回复
热议问题