如何使用T-SQL将十六进制颜色转换为RGB颜色

时间:2010-06-21 19:40:09

标签: sql tsql

如何使用T-SQL

将十六进制颜色转换为RGB颜色

2 个答案:

答案 0 :(得分:5)

https://data.stackexchange.com/stackoverflow/query/4803/so3087921

DECLARE @temp AS TABLE (hex char(6))

INSERT  INTO @temp
VALUES  ('3333CC') -- Should convert to Red: 51 Green: 51 Blue: 204

DECLARE @table AS varchar(16)
SET @table = '0123456789abcdef' -- Assuming case-insensitive collation!

SELECT  hex
       ,16 * (CHARINDEX(SUBSTRING(hex, 1, 1), @table) - 1) + (CHARINDEX(SUBSTRING(hex, 2, 1), @table) - 1) AS R
       ,16 * (CHARINDEX(SUBSTRING(hex, 3, 1), @table) - 1) + (CHARINDEX(SUBSTRING(hex, 4, 1), @table) - 1) AS G
       ,16 * (CHARINDEX(SUBSTRING(hex, 5, 1), @table) - 1) + (CHARINDEX(SUBSTRING(hex, 6, 1), @table) - 1) AS B
FROM    @temp

https://dpatrickcaldwell.blogspot.com/2009/05/converting-hexadecimal-or-binary-to.html

有一个通用函数

如果可能的话,我通常会避免使用标量函数,因为有可能引诱自己认为将标量UDF调用500万行而不会对内联表达式造成任何惩罚。

您还可以使用内联表值UDF:

CREATE FUNCTION dbo.udf_HexToRGB (@hex char(6))
RETURNS TABLE
AS RETURN
    (
     SELECT 16 * (CHARINDEX(SUBSTRING(@hex, 1, 1), '0123456789abcdef') - 1) + (CHARINDEX(SUBSTRING(@hex, 2, 1),
                                                                                         '0123456789abcdef') - 1) AS R
           ,16 * (CHARINDEX(SUBSTRING(@hex, 3, 1), '0123456789abcdef') - 1) + (CHARINDEX(SUBSTRING(@hex, 4, 1),
                                                                                         '0123456789abcdef') - 1) AS G
           ,16 * (CHARINDEX(SUBSTRING(@hex, 5, 1), '0123456789abcdef') - 1) + (CHARINDEX(SUBSTRING(@hex, 6, 1),
                                                                                         '0123456789abcdef') - 1) AS B
    )
GO

DECLARE @temp AS TABLE (hex char(6)) 

INSERT  INTO @temp 
VALUES  ('3333CC') -- Should convert to Red: 51 Green: 51 Blue: 204 

SELECT  hex 
       ,R 
       ,G 
       ,B 
FROM    @temp
OUTER APPLY dbo.udf_HexToRGB(hex)

答案 1 :(得分:3)

这是我刚写完的一个表值函数。它将十六进制颜色值作为char(6)值,并返回包含RGB列的表格。如果您不在SQL服务器上,则应该很容易适应您的数据库。

-- =============================================
-- Author:      Josh Thompson
-- Create date: 2010-06-21
-- Description: Convert hex color to RGB
-- =============================================
CREATE FUNCTION [fn_hex_to_rgb](
    @hex char(6)
    )
RETURNS TABLE 
AS
RETURN (
    SELECT
        (
            (
                CASE LOWER(SUBSTRING(@hex, 1, 1))
                    WHEN 'a'
                        THEN 10
                    WHEN 'b'
                        THEN 11
                    WHEN 'c'
                        THEN 12
                    WHEN 'd'
                        THEN 13
                    WHEN 'e'
                        THEN 14
                    WHEN 'f'
                        THEN 15
                        ELSE CAST(SUBSTRING(@hex, 1, 1) AS int)
                    END
                *
                16
                )
            +
            (
                CASE LOWER(SUBSTRING(@hex, 2, 1))
                    WHEN 'a'
                        THEN 10
                    WHEN 'b'
                        THEN 11
                    WHEN 'c'
                        THEN 12
                    WHEN 'd'
                        THEN 13
                    WHEN 'e'
                        THEN 14
                    WHEN 'f'
                        THEN 15
                        ELSE CAST(SUBSTRING(@hex, 2, 1) AS int)
                    END
                )
            ) AS R,
        (
            (
                CASE LOWER(SUBSTRING(@hex, 3, 1))
                    WHEN 'a'
                        THEN 10
                    WHEN 'b'
                        THEN 11
                    WHEN 'c'
                        THEN 12
                    WHEN 'd'
                        THEN 13
                    WHEN 'e'
                        THEN 14
                    WHEN 'f'
                        THEN 15
                        ELSE CAST(SUBSTRING(@hex, 3, 1) AS int)
                    END
                *
                16
                )
            +
            (
                CASE LOWER(SUBSTRING(@hex, 4, 1))
                    WHEN 'a'
                        THEN 10
                    WHEN 'b'
                        THEN 11
                    WHEN 'c'
                        THEN 12
                    WHEN 'd'
                        THEN 13
                    WHEN 'e'
                        THEN 14
                    WHEN 'f'
                        THEN 15
                        ELSE CAST(SUBSTRING(@hex, 4, 1) AS int)
                    END
                )
            ) AS G,
        (
            (
                CASE LOWER(SUBSTRING(@hex, 5, 1))
                    WHEN 'a'
                        THEN 10
                    WHEN 'b'
                        THEN 11
                    WHEN 'c'
                        THEN 12
                    WHEN 'd'
                        THEN 13
                    WHEN 'e'
                        THEN 14
                    WHEN 'f'
                        THEN 15
                        ELSE CAST(SUBSTRING(@hex, 5, 1) AS int)
                    END
                *
                16
                )
            +
            (
                CASE LOWER(SUBSTRING(@hex, 6, 1))
                    WHEN 'a'
                        THEN 10
                    WHEN 'b'
                        THEN 11
                    WHEN 'c'
                        THEN 12
                    WHEN 'd'
                        THEN 13
                    WHEN 'e'
                        THEN 14
                    WHEN 'f'
                        THEN 15
                        ELSE CAST(SUBSTRING(@hex, 6, 1) AS int)
                    END
                )
            ) AS B
    )
GO