Domanda

I need to display some data in an SSRS 2008r2 report and the colors have to match a Windows VB app that saves it's colors as integers (e.g.16744703 is a pinkish color). I believe this is ARGB format. I'm not concerned about the alpha value, as the application does not allow the user to modify it.

I'm stuck on the SQL to convert ARGB to something compatible in SSRS. I need to do the translation in SQL as there are other factors that may override an objects color.

I can work with 3 ints for rgb or a hex value

Anyone got any idea how tot do this?

Regards

mark

È stato utile?

Soluzione

Figured it out. Here's a function that returs either RGB() or Hex

   -- Description:  Converts ARGB to RGB(RR,GG,BB)
   --               e.g. 16744703 returns RGB(255,128,255) or #FF80FF   
   CREATE FUNCTION [dbo].[ARGB2RGB] 
    (
        @ARGB AS BIGINT 
       ,@ColorType AS VARCHAR(1)   -- 'H' = Hex, 'R' = RGB
    )
    RETURNS VARCHAR(16)
    AS
    BEGIN
        DECLARE @Octet1 TINYINT
        DECLARE @Octet2 TINYINT
        DECLARE @Octet3 TINYINT
        DECLARE @Octet4 TINYINT
        DECLARE @RestOfColor BIGINT

        SET @Octet1 = @ARGB / 16777216
        SET @RestOfColor = @ARGB - ( @Octet1 * CAST(16777216 AS BIGINT) )
        SET @Octet2 = @RestOfColor / 65536
        SET @RestOfColor = @RestOfColor - ( @Octet2 * 65536 )
        SET @Octet3 = @RestOfColor / 256
        SET @Octet4 = @RestOfColor - ( @Octet3 * 256 )

        RETURN
            CASE @ColorType
              WHEN 'R'
              THEN 'RGB(' + CONVERT(VARCHAR, @Octet4) + ','
                   + CONVERT(VARCHAR, @Octet3) + ',' + CONVERT(VARCHAR, @Octet2)
                   + ')'
              WHEN 'H'
              THEN '#' + RIGHT(sys.fn_varbintohexstr(@Octet4), 2)
                   + RIGHT(sys.fn_varbintohexstr(@Octet3), 2)
                   + RIGHT(sys.fn_varbintohexstr(@Octet2), 2)
            END 
    END

Hope someone else finds it useful

Regards

Mark

Altri suggerimenti

create FUNCTION [dbo].[ConvertRGB]
(
     @ARGB AS float 
)
RETURNS @ReturnValue TABLE ( R TINYINT,B TINYINT, G TINYINT )
as
BEGIN
    DECLARE @testvarbinary binary(4)
    DECLARE @strRBG nvarchar(MAX)
    set  @testvarbinary = CONVERT(binary(4),@ARGB)
    set @strRBG=( SELECT  substring(sys.fn_varbintohexstr(@testvarbinary),5,6))
    DECLARE @temp AS TABLE (hex char(6))
    INSERT  INTO @temp
    VALUES  (@strRBG) 
    DECLARE @strHex AS varchar(16)
    SET @strHex = '0123456789abcdef' -- Assuming case-insensitive collation!
    INSERT  INTO @ReturnValue
            ( R,G,B )
            SELECT   16 * (CHARINDEX(SUBSTRING(hex, 1, 1), @strHex) - 1) + (CHARINDEX(SUBSTRING(hex, 2, 1), @strHex) - 1) 
                    ,16 * (CHARINDEX(SUBSTRING(hex, 3, 1), @strHex) - 1) + (CHARINDEX(SUBSTRING(hex, 4, 1), @strHex) - 1) 
                    ,16 * (CHARINDEX(SUBSTRING(hex, 5, 1), @strHex) - 1) + (CHARINDEX(SUBSTRING(hex, 6, 1), @strHex) - 1) 
            FROM    @temp                 
    RETURN  
END;

GO
--select * from [ConvertRGB](10592513)

I was looking for doing something like this and came up with this post. It was of great help but I found it more appealing doing it inline as follow. Notice that the result can be used inside the SSRS Color Expression with no additional conversions

select CustomerID
  ,SSRSColor    = '#' + SUBSTRING(S.TXTHEXColor, 5, 2) 
                      + SUBSTRING(S.TXTHEXColor, 3, 2) 
                      + SUBSTRING(S.TXTHEXColor, 1, 2)
from 
(
Select  CustomerID
       ,[TXTHEXColor] = right(sys.fn_varbintohexstr(CONVERT(varbinary, T.Color)), 6)
 From SomeTable T
) S

If a function is still needed then following is a much shorter way but bear in mind that with big record sets functions can slow down the process a lot.

CREATE FUNCTION [dbo].[SSRSColor] (
    @ARGB AS INT 
)
RETURNS VARCHAR(7)
AS
BEGIN 
    DECLARE @TXTHEXColor varchar(100)

    Select  @TXTHEXColor = right(sys.fn_varbintohexstr(CONVERT(varbinary, @ARGB)), 6)

    return '#' + SUBSTRING(@TXTHEXColor, 5, 2) + SUBSTRING(@TXTHEXColor, 3, 2) + SUBSTRING(@TXTHEXColor, 1, 2)
END
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top