将颜色名称转换为 RGB

发布于 2024-11-02 19:53:37 字数 197 浏览 3 评论 0原文

我有一个记录表,每个记录都存储一个颜色名称。例如:

Product    |  Colour
-------------------
Product A  |  Blue
Product B  |  Black

我添加了 3 个新列:R、G & B. 如何使用单个 SQL 查询将颜色转换为 RGB 值?

I have a table of records which each store a Colour Name. e.g:

Product    |  Colour
-------------------
Product A  |  Blue
Product B  |  Black

I have added 3 new columns: R,G & B. How can I convert the Colours into RGB values using a single SQL query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

因为看清所以看轻 2024-11-09 19:53:37
;with Colours(Name, R, G, B) as
(
  select 'White',   255, 255, 255 union all
  select 'Silver',  192, 192, 192 union all
  select 'Gray',    128, 128, 128 union all
  select 'Black',   0  , 0  , 0   union all
  select 'Red',     255, 0  , 0   union all
  select 'Maroon',  128, 0  , 0   union all
  select 'Yellow',  255, 255, 0   union all
  select 'Olive',   128, 128, 0   union all
  select 'Lime',    0  , 255, 0   union all
  select 'Green',   0  , 128, 0   union all
  select 'Aqua',    0  , 255, 255 union all
  select 'Teal',    0  , 128, 128 union all
  select 'Blue',    0  , 0  , 255 union all
  select 'Navy',    0  , 0  , 128 union all
  select 'Fuchsia', 255, 0  , 255 union all
  select 'Purple',  128, 0  , 128
)
update P set
  R = C.R,
  G = C.G,
  B = C.B
from products as P
  inner join Colours as C
    on P.Colour = C.Name
;with Colours(Name, R, G, B) as
(
  select 'White',   255, 255, 255 union all
  select 'Silver',  192, 192, 192 union all
  select 'Gray',    128, 128, 128 union all
  select 'Black',   0  , 0  , 0   union all
  select 'Red',     255, 0  , 0   union all
  select 'Maroon',  128, 0  , 0   union all
  select 'Yellow',  255, 255, 0   union all
  select 'Olive',   128, 128, 0   union all
  select 'Lime',    0  , 255, 0   union all
  select 'Green',   0  , 128, 0   union all
  select 'Aqua',    0  , 255, 255 union all
  select 'Teal',    0  , 128, 128 union all
  select 'Blue',    0  , 0  , 255 union all
  select 'Navy',    0  , 0  , 128 union all
  select 'Fuchsia', 255, 0  , 255 union all
  select 'Purple',  128, 0  , 128
)
update P set
  R = C.R,
  G = C.G,
  B = C.B
from products as P
  inner join Colours as C
    on P.Colour = C.Name
牵你的手,一向走下去 2024-11-09 19:53:37

您需要自己添加相应名称的 RGB 值,无法从颜色名称中获取该信息(毕竟什么构成了“深蓝色”?)。

您可以使用 CSS 名称作为构建初始查找表的提示。

You will need to add the RGB values for the corresponding name yourself, there is no way to derive that information from a colour name (after all what constitutes "Dark Blue"?).

You could use the CSS names for hints to build the initial look-up table.

独守阴晴ぅ圆缺 2024-11-09 19:53:37

您需要在用于更新表的同一查询中将颜色解码为 RGB 值。当您没有使用任何函数、变量或其他表时,这可以使用 CASE 表达式来完成。这不是优雅的解决方案,但它会起作用。

UPDATE products
SET
    r = (CASE colour
            WHEN 'Black' THEN 0
            WHEN 'Red' THEN 255
            WHEN 'Green' THEN 0
            WHEN 'Blue' THEN 0
            WHEN 'White' THEN 255
            ELSE NULL
        END),
    g = (CASE colour
            WHEN 'Black' THEN 0
            WHEN 'Red' THEN 0
            WHEN 'Green' THEN 255
            WHEN 'Blue' THEN 0
            WHEN 'White' THEN 255
            ELSE NULL
        END),
    b = (CASE colour
            WHEN 'Black' THEN 0
            WHEN 'Red' THEN 0
            WHEN 'Green' THEN 0
            WHEN 'Blue' THEN 255
            WHEN 'White' THEN 255
            ELSE NULL
        END)

或者也许通过以下方式:

UPDATE products
SET
    r = (CASE
            WHEN Colour IN ('Black', 'Green', 'Blue') THEN 0
            WHEN Colour IN ('Red', 'White') THEN 255
            ELSE NULL
        END),
    g = (CASE
            WHEN Colour IN ('Black', 'Red', 'Blue') THEN 0
            WHEN Colour IN ('Green', 'White') THEN 255
            ELSE NULL
        END),
    b = (CASE
            WHEN Colour IN ('Black', 'Red', 'Green') THEN 0
            WHEN Colour IN ('Blue', 'White') THEN 255
            ELSE NULL
        END)

You need to decode colours to RGB values in the same query which is used to update your table. While you are not using any functions, variables or another tables this can be done using CASE expression. This is not elegant solution but it will work.

UPDATE products
SET
    r = (CASE colour
            WHEN 'Black' THEN 0
            WHEN 'Red' THEN 255
            WHEN 'Green' THEN 0
            WHEN 'Blue' THEN 0
            WHEN 'White' THEN 255
            ELSE NULL
        END),
    g = (CASE colour
            WHEN 'Black' THEN 0
            WHEN 'Red' THEN 0
            WHEN 'Green' THEN 255
            WHEN 'Blue' THEN 0
            WHEN 'White' THEN 255
            ELSE NULL
        END),
    b = (CASE colour
            WHEN 'Black' THEN 0
            WHEN 'Red' THEN 0
            WHEN 'Green' THEN 0
            WHEN 'Blue' THEN 255
            WHEN 'White' THEN 255
            ELSE NULL
        END)

Or maybe in the following way:

UPDATE products
SET
    r = (CASE
            WHEN Colour IN ('Black', 'Green', 'Blue') THEN 0
            WHEN Colour IN ('Red', 'White') THEN 255
            ELSE NULL
        END),
    g = (CASE
            WHEN Colour IN ('Black', 'Red', 'Blue') THEN 0
            WHEN Colour IN ('Green', 'White') THEN 255
            ELSE NULL
        END),
    b = (CASE
            WHEN Colour IN ('Black', 'Red', 'Green') THEN 0
            WHEN Colour IN ('Blue', 'White') THEN 255
            ELSE NULL
        END)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文