How do I convert a Hex color t开发者_高级运维o and RGB color using T-SQL
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
There's a generic function at https://dpatrickcaldwell.blogspot.com/2009/05/converting-hexadecimal-or-binary-to.html
I generally avoid scalar functions if at all possible, since it is possible to lure yourself into thinking that it's OK to call a scalar UDF for 5 million rows without any penalty over the inline expression.
You can also use an inline table-valued 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)
This is a table valued function I just wrote up. It takes the hex color value as a char(6)
value and returns a table with R
, G
, and B
columns. If you aren't on SQL server, it should be easy to adapt to your database.
-- =============================================
-- 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
精彩评论