gpt4 book ai didi

sql - Postgres 9.4 : fast way to convert color from hex to rgb representation

转载 作者:行者123 更新时间:2023-11-29 11:41:37 28 4
gpt4 key购买 nike

我有十六进制表示颜色,示例 #112233 存储在 Postgresql 9.4

需要在 pl/pgsql 函数中将此表示形式转换为 rgb(17,34,51)

有什么想法可以最快地转换它吗?

最佳答案

这使用 Erwin's trick将十六进制值转换为整数值:

with colors (hex_code) as (
values ('#112233'), ('#203040')
)
select 'rgb('||
('x'||substr(hex_code,2,2))::bit(8)::int||','||
('x'||substr(hex_code,4,2))::bit(8)::int||','||
('x'||substr(hex_code,6,2))::bit(8)::int||')'
from colors
;

不确定这是否是最快的方法,但我想不出其他方法。 select 表达式可以毫无问题地移动到函数中。

SQLFiddle 演示:http://sqlfiddle.com/#!15/d41d8/3720

关于sql - Postgres 9.4 : fast way to convert color from hex to rgb representation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26441036/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com