在SQL数据库中表示颜色的最佳方法?

时间:2022-06-13 08:07:33

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?

如果我使用的是.Net和SQL Server 2008,那么在数据库中存储颜色的最佳方式是什么?我应该使用ToString还是将其转换为整数或其他什么?

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 个解决方案

#1


16  

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

如果您只是使用0xRRGGBB格式,您也可以将其作为整数存储在数据库中,并在SELECT时重新进行缩放(为了便于阅读)。

#2


11  

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.

如何在数据库中存储信息取决于您打算如何使用和访问它。没有必要说明存储它的最佳方法是不知道你将如何使用它。

#3


6  

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?

在某些行业,如数码相机,桌面出版,sanners和其他行业,色彩可能会非常棘手。大多数程序员将颜色与24位颜色(通常为RGB)相关联,有些则将其与32位(RGBA)相关联。在像DTP这样大量使用颜色的行业中工作的少数人有更丰富的术语,包括色彩校正,色彩空间等等。那么你究竟需要存储什么?

  • 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.
  • 您是否需要存储多种格式并知道实际存储的格式(RGB与RGBA对比CMY与HSV etv)?请注意,即使像RGB一样简单的东西实际上也可以是Adobe RGB或sRGB。
  • Do you need to store color space and correction? Note often the RGB color has no meaning w/o proper color management.
  • 你需要存储色彩空间和校正吗?请注意,RGB颜色通常没有适当的颜色管理意义。
  • 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) ?
  • 您是否需要存储“网页”颜色(即RGB或RGBA为十六进制)?

#4


3  

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.

如果要存储System.Drawing.Color,则需要存储表示alpha和3色通道的4个字节。您可以使用int数据类型。

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

如果要存储System.Windows.Media.Color(来自WPF),根据您使用的用法,有两种可能:

  1. If you are only using ARGB colors, store as a int data type.
  2. 如果您只使用ARGB颜色,请存储为int数据类型。
  3. If you are using the ScRGB format, you need to store 4 float (single) values. I suggest two way of storing it:
  4. 如果您使用的是ScRGB格式,则需要存储4个浮点(单个)值。我建议两种存储方式:
    1. A user-defined type with 4 float fields.
    2. 用户定义的类型,包含4个浮点字段。
    3. A varchar (length depend on the precision) and storing
      color.ToString(CultureInfo.InvariantCulture)
    4. varchar(长度取决于精度)和存储颜色.ToString(CultureInfo.InvariantCulture)

#5


2  

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

好吧,我将它们存储为六位十六进制代码。这开启了实际搜索其他颜色之间颜色的有趣可能性。如果你真的想让它变得强大,请将R,G,B十六进制数保存在三列中。这样可以找到包含太多红色的颜色,或者与另一种颜色相似的颜色(按三个值的平均差异排序)。

#6


1  

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.

取决于您想要存储的颜色类型。例如。如果它来自固定的调色板,则short int或甚至无符号字节可能就足够了。

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.

3字节RGB或4字节ARGB(A = alpha,即透明度)可以适合32位,因此32位无符号整数类型可以工作。这是大多数应用程序的标准。但是,您可能需要更多 - 在这种情况下可能需要64位无符号整数。

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

或者,如果您想根据其组件修改或查询颜色,我会将每个组件存储为其自己的unsigned int(即红色,绿色,蓝色,Alpha字段)。

#1


16  

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

如果您只是使用0xRRGGBB格式,您也可以将其作为整数存储在数据库中,并在SELECT时重新进行缩放(为了便于阅读)。

#2


11  

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.

如何在数据库中存储信息取决于您打算如何使用和访问它。没有必要说明存储它的最佳方法是不知道你将如何使用它。

#3


6  

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?

在某些行业,如数码相机,桌面出版,sanners和其他行业,色彩可能会非常棘手。大多数程序员将颜色与24位颜色(通常为RGB)相关联,有些则将其与32位(RGBA)相关联。在像DTP这样大量使用颜色的行业中工作的少数人有更丰富的术语,包括色彩校正,色彩空间等等。那么你究竟需要存储什么?

  • 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.
  • 您是否需要存储多种格式并知道实际存储的格式(RGB与RGBA对比CMY与HSV etv)?请注意,即使像RGB一样简单的东西实际上也可以是Adobe RGB或sRGB。
  • Do you need to store color space and correction? Note often the RGB color has no meaning w/o proper color management.
  • 你需要存储色彩空间和校正吗?请注意,RGB颜色通常没有适当的颜色管理意义。
  • 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) ?
  • 您是否需要存储“网页”颜色(即RGB或RGBA为十六进制)?

#4


3  

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.

如果要存储System.Drawing.Color,则需要存储表示alpha和3色通道的4个字节。您可以使用int数据类型。

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

如果要存储System.Windows.Media.Color(来自WPF),根据您使用的用法,有两种可能:

  1. If you are only using ARGB colors, store as a int data type.
  2. 如果您只使用ARGB颜色,请存储为int数据类型。
  3. If you are using the ScRGB format, you need to store 4 float (single) values. I suggest two way of storing it:
  4. 如果您使用的是ScRGB格式,则需要存储4个浮点(单个)值。我建议两种存储方式:
    1. A user-defined type with 4 float fields.
    2. 用户定义的类型,包含4个浮点字段。
    3. A varchar (length depend on the precision) and storing
      color.ToString(CultureInfo.InvariantCulture)
    4. varchar(长度取决于精度)和存储颜色.ToString(CultureInfo.InvariantCulture)

#5


2  

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

好吧,我将它们存储为六位十六进制代码。这开启了实际搜索其他颜色之间颜色的有趣可能性。如果你真的想让它变得强大,请将R,G,B十六进制数保存在三列中。这样可以找到包含太多红色的颜色,或者与另一种颜色相似的颜色(按三个值的平均差异排序)。

#6


1  

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.

取决于您想要存储的颜色类型。例如。如果它来自固定的调色板,则short int或甚至无符号字节可能就足够了。

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.

3字节RGB或4字节ARGB(A = alpha,即透明度)可以适合32位,因此32位无符号整数类型可以工作。这是大多数应用程序的标准。但是,您可能需要更多 - 在这种情况下可能需要64位无符号整数。

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

或者,如果您想根据其组件修改或查询颜色,我会将每个组件存储为其自己的unsigned int(即红色,绿色,蓝色,Alpha字段)。