KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Converting from 4D's color palette to RGB
PRODUCT: 4D | VERSION: 11 | PLATFORM: Mac & Win
Published On: August 6, 2009

Assume that you want to store a color from 4D's color palette (1-256) for programmatic use in your code. The following code converts an indexed color from 4D's color palette (1-256) to an RGB longint value (0x00RRGGBB).

 `Util_ColorIndexToRGB( Ndx_I ) -> RGB_LC_LONGINT(\$0;\$RGB_L)C_LONGINT(\$Ndx;\$1)C_LONGINT(\$ChartColor_L;\$Red_L;\$Green_L;\$Blue_L)\$Ndx:=\$1\$ChartColor_L:=CT Index to color (\$Ndx) ` Extract the RGB values. CT COLOR TO RGB returns values from 0..65535 ` in the format of 0x00bbggrr, not in the needed encoding of 0x00rrggbb\$Red_L:=(\$ChartColor_L & 0x00FF) ` get the red byte, 0..255\$Green_L:=((\$ChartColor_L >> 8) & 0x00FF) ` get the green byte, 0..255\$Blue_L:=((\$ChartColor_L >> 16) & 0x00FF) ` get the blue byte, 0..255 ` Combine the RGB values to create a 4D RGB (0x00rrggbb) value\$RGB_L:=(\$Red_L << 16)+(\$Green_L << 8)+\$Blue_L\$0:=\$RGB_L

Use of the above methods would look like this:

 C_LONGINT(\$ColorNdx_L;\$RGB_L)\$ColorNdx_L:=122 ` Any value from 1-256\$RGB_L:=Util_ColorIndexToRGB( \$ColorNdx_L )

Commented by Charlie Vass on August 12, 2009 at 10:00 AM
The method "Util_ColorIndexToRGB" is further demonstrated in asset #75865