A quick article to document a Zoho Deluge function converting a hexadecimal color reference to a Red Green Blue value (RGB).
Why?
It's likely that Zoho will avail their color picker at some point as an input but at the time of print, this hasn't happened yet. Here's a function in deluge that takes a 3 or 6 character hexadecimal color and returns the RGB values for you.
How?
Very quickly, we take each character from the given string and convert it to its RGB value. Replace letters A to F with their numeric equivalents. Made up of 2 characters to convert to an RGB subset, the 1st value is to be multiplied by 16 and then added to the value of the 2nd; this equals the RGB value.
Given #00FF00
// // given value v_HexGiven = "#00FF00"; // // transform or format the submitted value v_HexValue = v_HexGiven.replaceAll("#", "").toUpperCase(); // // using a regular expression: split into pairs of characters or if short hex given, then split into 3 characters l_HexParts = v_HexValue.replaceAll("(?)",",",false).toList().subList(1,4); if(v_HexGiven.toString().len()>4) { l_HexParts = v_HexValue.replaceAll("(?<=\\G..)",",",false).toList().subList(0,3); } // // declare our output variable and map the characters to a value l_RgbResult = List(); m_HexMap = {"0":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9,"A":10,"B":11,"C":12,"D":13,"E":14,"F":15}; // // loop through the 3 parts of the given hex and add to a list for each v_HexPart in l_HexParts { v_HexConvert = if(v_HexPart.toString().len()==1, v_HexPart.toString() + v_HexPart.toString(), v_HexPart).toString(); v_Rgb_1 = m_HexMap.get(v_HexConvert.subString(0,1)) * 16; v_Rgb_2 = m_HexMap.get(v_HexConvert.subString(1,2)); v_Rgb_Sum = v_Rgb_1 + v_Rgb_2; l_RgbResult.add(v_Rgb_Sum); } // // output info "rgb(" + l_RgbResult.toList() + ")"; // yields rgb(0,255,0)
- //
- // given value
- v_HexGiven = "#00FF00";
- //
- // transform or format the submitted value
- v_HexValue = v_HexGiven.replaceAll("#", "").toUpperCase();
- //
- // using a regular expression: split into pairs of characters or if short hex given, then split into 3 characters
- l_HexParts = v_HexValue.replaceAll("(?)",",",false).toList().subList(1,4);
- if(v_HexGiven.toString().len()>4)
- {
- l_HexParts = v_HexValue.replaceAll("(?<=\\G..)",",",false).toList().subList(0,3);
- }
- //
- // declare our output variable and map the characters to a value
- l_RgbResult = List();
- m_HexMap = {"0":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9,"A":10,"B":11,"C":12,"D":13,"E":14,"F":15};
- //
- // loop through the 3 parts of the given hex and add to a list
- for each v_HexPart in l_HexParts
- {
- v_HexConvert = if(v_HexPart.toString().len()==1, v_HexPart.toString() + v_HexPart.toString(), v_HexPart).toString();
- v_Rgb_1 = m_HexMap.get(v_HexConvert.subString(0,1)) * 16;
- v_Rgb_2 = m_HexMap.get(v_HexConvert.subString(1,2));
- v_Rgb_Sum = v_Rgb_1 + v_Rgb_2;
- l_RgbResult.add(v_Rgb_Sum);
- }
- //
- // output
- info "rgb(" + l_RgbResult.toList() + ")";
- // yields rgb(0,255,0)
Note(s):
If using this in CRM, note that the regular expression here needs the escaping(-escaping) backslash to be omitted:
l_HexParts = v_HexValue.replaceAll("(?<=\G..)",",",false).toList().subList(0,3);
- l_HexParts = v_HexValue.replaceAll("(?<=\G..)",",",false).toList().subList(0,3);
Testing it out
v_HexGiven = "#0f0"; // yields: rgb(0,255,0) v_HexGiven = "#dcf02c"; // yields: rgb(220,240,44)
- v_HexGiven = "#0f0";
- // yields: rgb(0,255,0)
- v_HexGiven = "#dcf02c";
- // yields: rgb(220,240,44)
Broken Down / Simplified:
v_1stHex = v_HexGiven.subString(0,2); v_2ndHex = v_HexGiven.subString(2,4); v_3rdHex = v_HexGiven.subString(4,6); // v_Rgb_1 = m_HexNumbers.get(v_1stHex.subString(0,1)) * 16; v_Rgb_2 = m_HexNumbers.get(v_1stHex.subString(1,2)); v_1stRgb = v_Rgb_1 + v_Rgb_2; // v_Rgb_1 = m_HexNumbers.get(v_2ndHex.subString(0,1)) * 16; v_Rgb_2 = m_HexNumbers.get(v_2ndHex.subString(1,2)); v_2ndRgb = v_Rgb_1 + v_Rgb_2; // v_Rgb_1 = m_HexNumbers.get(v_3rdHex.subString(0,1)) * 16; v_Rgb_2 = m_HexNumbers.get(v_3rdHex.subString(1,2)); v_3rdRgb = v_Rgb_1 + v_Rgb_2; // info "rgb(" + v_1stRgb + "," + v_2ndRgb + "," + v_3rdRgb + ")";
- v_1stHex = v_HexGiven.subString(0,2);
- v_2ndHex = v_HexGiven.subString(2,4);
- v_3rdHex = v_HexGiven.subString(4,6);
- //
- v_Rgb_1 = m_HexNumbers.get(v_1stHex.subString(0,1)) * 16;
- v_Rgb_2 = m_HexNumbers.get(v_1stHex.subString(1,2));
- v_1stRgb = v_Rgb_1 + v_Rgb_2;
- //
- v_Rgb_1 = m_HexNumbers.get(v_2ndHex.subString(0,1)) * 16;
- v_Rgb_2 = m_HexNumbers.get(v_2ndHex.subString(1,2));
- v_2ndRgb = v_Rgb_1 + v_Rgb_2;
- //
- v_Rgb_1 = m_HexNumbers.get(v_3rdHex.subString(0,1)) * 16;
- v_Rgb_2 = m_HexNumbers.get(v_3rdHex.subString(1,2));
- v_3rdRgb = v_Rgb_1 + v_Rgb_2;
- //
- info "rgb(" + v_1stRgb + "," + v_2ndRgb + "," + v_3rdRgb + ")";
Vice-Versa: Code Snippet to convert RGB to Hex
Even easier than our Hex to RGB, same as above but in reverse:
// // given value (note that a list of 3 values are submitted to this) l_RgbRequest = ifnull(p_RGB,{0,0,0}); // // list to map the value to a character l_RgbList = {0,1,2,3,4,5,6,7,8,9,"A","B","C","D","E","F"}; // // loop through the 3 parts of the given hex and add to a list v_HexResult = "#"; for each v_RgbPart in l_RgbRequest { // // quotient: divide rgb by 16 for 1st character v_Hex_1 = floor(v_RgbPart / 16); // // remainder/modulus: get remainder from division of rgb by 16 v_Hex_2 = v_RgbPart % 16; // // concatenate these 2 characters to our output string v_HexResult = v_HexResult + l_RgbList.get(v_Hex_1).toString() + l_RgbList.get(v_Hex_2).toString(); } // // output return v_HexResult;
- //
- // given value (note that a list of 3 values are submitted to this)
- l_RgbRequest = ifnull(p_RGB,{0,0,0});
- //
- // list to map the value to a character
- l_RgbList = {0,1,2,3,4,5,6,7,8,9,"A","B","C","D","E","F"};
- //
- // loop through the 3 parts of the given hex and add to a list
- v_HexResult = "#";
- for each v_RgbPart in l_RgbRequest
- {
- //
- // quotient: divide rgb by 16 for 1st character
- v_Hex_1 = floor(v_RgbPart / 16);
- //
- // remainder/modulus: get remainder from division of rgb by 16
- v_Hex_2 = v_RgbPart % 16;
- //
- // concatenate these 2 characters to our output string
- v_HexResult = v_HexResult + l_RgbList.get(v_Hex_1).toString() + l_RgbList.get(v_Hex_2).toString();
- }
- //
- // output
- return v_HexResult;
// given l_Given = {11,255,128}; info thisapp.Color_Picker.fn_RGBToHex(l_Given); // yields #0BFF80
- // given
- l_Given = {11,255,128};
- info thisapp.Color_Picker.fn_RGBToHex(l_Given);
- // yields #0BFF80