KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: A utility method for converting RGBHex to RGBDecimal
PRODUCT: 4D | VERSION: 12.3 | PLATFORM: Mac & Win
Published On: February 16, 2012

Sometimes it is necessary to convert an RGB Hex string into its longinteger form. The utility below, UTIL_RGBHex_To_RGBDecimal, will accept an RGB string in the form of #RRGGBB or 0x00RRGGBB and convert it into a longinteger.


If (True)
  If (False)
    Begin SQL
      /*
      UTIL_RGBHex_To_RGBDecimal

      Purpose: Convert an RGB string into an RGB value

      $0 - LONGINT - RGB in Base 10
      $1 - TEXT - RGB hex string
      */

    End SQL
  End if
  C_TEXT($MethodName_T)
  $MethodName_T:=Current method name
  //============ Declare Variables ============
  //method_parameters_declarations

  C_LONGINT($0;$RGBDecimal_L)
  C_TEXT($RGBHex_T;$1)
    //-----------------------------------------
    //local_variable_declarations
  C_LONGINT($Ndx;$SOA;$RIS;$Len_L)
End if
  //============ Initialize and Setup ============

$RGBHex_T:=$1
$SOA:=Position("#";$RGBHex_T;1;$Len_L)
If ($SOA>0)
  $RGBHex_T:=Substring($RGBHex_T;$SOA+1)
Else
  $SOA:=Position("0x";$RGBHex_T;1;$Len_L)
  If ($SOA>0)
    $RGBHex_T:=Substring($RGBHex_T;$SOA+2)
  End if
End if

  //============ Method Actions ============

For ($Ndx;1;Length($RGBHex_T))
  $RGBDecimal_L:=($RGBDecimal_L << 4)+\
    Position($RGBHex_T[[$Ndx]];"123456789ABCDEF";1;$Len_L)
End for

  //============ Clean up and Exit ============

$0:=$RGBDecimal_L