KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Using Color Picker to Return the RGB Value
PRODUCT: 4D | VERSION: 15.3 | PLATFORM: Mac & Win
Published On: March 30, 2017

4D allows the use of a color picker to modify a select number of items such as variable objects. when using the color picker the color gets applied but does not return anything. It is possible to obtain the RGB Hex Value of the selected color by code using native 4D features. Below provides steps to create a utility method that will perform the task.

1) Create a Form that will be used exclusively for this feature.
2) Add a variable object
3) In the properties for the object enable the "Allow font/color picker" option
4) Also make sure that Multistyled Text is NOT enabled
5) For aestetic purposes set the dimensions of the object to 0 by 0 and place it at the 0,0 coordinates with the form's margins set to 0.

4) Add the following code to the form method:

// Form Method
// Variable Object Name: Variable


Case of
    : (Form event=On Load)
       C_LONGINT($temp;colorNum)
       colorNum:=-1
       OPEN COLOR PICKER(1)
       ACCEPT
    : (Form event=On After Edit)
   //Make sure the variable name is correct.
       OBJECT GET RGB COLORS(*;"Variable";$temp;colorNum)
       ACCEPT
End case

Also make sure that the created variable object in step 2 is correctly referenced in line 12 of the code as mentioned.

Next Create a Form method with the following code:
// Method: Util_Get_Color_Pick
//
// Details: Opens a Color Picker at specified location or defaults to upper left
//
// Form Referenced: Form1
//
// Input:
//    $1 - Longint Optional, Defaults to 0
//       Horizontal Possition of the Left side of the Color Picker
//    $2 - Longint Optional, Defaults to 0
//       Vertical Possition of the Top side of the Color Picker
//
// Output:
//    $0 - Longint
//       If color selected returns value of RGB Selected from Color Picker
//       Returns -1 it no color selected

C_LONGINT($1;$hPos_l)
C_LONGINT($2;$vPos_l)
C_LONGINT($0)
C_LONGINT($form;colorNum)
colorNum:=-1
//Make sure that the form name is correct.
IF(Count Parameters>1)
   $hPos_l:=$1
   $vPos_l:=$2
Else
   $hPos_l:=0
   $vPos_l:=0
End if
$form:=Open form window("Form1";Pop up form dialog box;$hPos_l;$vPos_l)
DIALOG("Form1")
CLOSE WINDOW($form)
$0:=colorNum

Also make sure that the created form in step 1 is correctly referenced in lines 16 and 17 of the code as mentioned.

Below is an example of calling the Utility Method:
C_LONGINT($colorValue)
$colorValue:=Util_Color_Pick(0;0)
TRACE

Calling the command will open the color picker:


If a color is selected the numerical RGB value is returned:
    Yellow Selected


    Blue Selected:


    Canceled:

This can be added to any 4D Database or even built into a component.