RGB calculation

  • Hello,

    I'm using SQL Server 2000 for data processing. I require some functions/methods to calculate RGB value. I'm having 4 fields which contains R, G & B values. I need to calculate the RGB(R,G,B) and store the resultant value in 4th Column.

    Is there any easy way to achieve this? As the database is very large,i can't use any programming languages.

    Tks in advance

    Venkat

  • What's the formula for calculating RGB from R, G, B? It's possible that T-SQL can do the maths.

    You could create a calculated column in the table, which means the RGB value is not stored, but is calculated when needed.

    Other option is you could create a 4th column and update the table whenever the R, G or B values change.

    Am I misunderstanding what you want?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Assuming your fields are called RGB for RGB value and R, G & B for individual parts then

    RGB = 65536 * R + 256 * G + B

    This is because RGB is designed to be three hex bytes of 0-255 colour values e.g. 0xFFFFFF = R=255(0xFF), G=255, B=255 - to shift the R value into the correct byte position times by 2^16, to shift G times by 2^8

    This may be a candidate for a calculated column rather than a fixed column as that way you don't need to recalc RGB each time R,G or B changes

  • Thanks James Horsley,

    Your assumption is correct. This is what i'm looking for.

    I will try your method and verify the results.

    Thanks once again

    Have a nice day!!!

    Regards

    Venkat

  • Hello James Horsley,

    i tried out ur calculation. I have made a minor change in the calculation to get the correct output.

    i have multiplied the 65536 with B value instead of R value. R is taken as is.

    New calculation is

    RGB = R + (256 * G) + (B*65536)

    The output of the above is exactly matching the output given by the RGB() function of the Windows API function.

    Thanks for ur valuable input.

    Venkat

    James Horsley (5/6/2008)


    Assuming your fields are called RGB for RGB value and R, G & B for individual parts then

    RGB = 65536 * R + 256 * G + B

    This is because RGB is designed to be three hex bytes of 0-255 colour values e.g. 0xFFFFFF = R=255(0xFF), G=255, B=255 - to shift the R value into the correct byte position times by 2^16, to shift G times by 2^8

    This may be a candidate for a calculated column rather than a fixed column as that way you don't need to recalc RGB each time R,G or B changes

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply