cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
Return a JMP Color Value from a Hex Color Code
txnelson
Super User

Many times color values are specified in many industries through hexadecimal numbers.

See:

     https://htmlcolorcodes.com/ 
This function facilitates the conversion of hexadecimal color values into JMP Color values

/**************************************************************/
/* Return the JMP Color number from an input of a 6 character */
/* hexadecimal string.                                        */
/* March 2020                                                 */
/* Jim Nelson                                                 */
/*                                                            */
/* Purpose: Many times color values are specified in many     */
/*          industries through hexadecimal numbers.           */
/*          See: https://htmlcolorcodes.com/                  */
/*          This function facilitates the conversion of       */
/*          hexadecimal color values into JMP Color values    */
/*                                                            */
/* Usage    return = HexToJMPColor( "HexVal");                */
/*                                                            */
/* Example  jmpColor = HexToJMPColor( "AA45AF");              */
/**************************************************************/

HexToJMPColor = Function( {HexString},
	{DefaultLocal}, 
	// Must be 6 characters in length, and contain only
	// the characters 0123456789ABCDEFabcdef
	If( Length( HexString ) == 6,
		errVal = "";
		For( i = 1, i <= 6, i++,
			If( Contains( "0123456789ABCDEFabcdef", Substr( HexString, i, 1 ) ) == 0,
				errVal = errVal || "2",
				errVal = errVal || "1"
			)
		);
		// If errors have not been found, calculate the JMP Color
		If( Contains( errVal, "2" ) == 0,
			JMPColor = RGB Color(
				(Hex To Number( Substr( HexString, 1, 2 ), 16 )) / 255,
				(Hex To Number( Substr( HexString, 3, 2 ), 16 )) / 255,
				(Hex To Number( Substr( HexString, 5, 2 ), 16 )) / 255
			)
		);
	,
		// If the number of input values is not 5 then return a list of 
		// 3's at the same lenght of input string
		errVal = Repeat( "3", Length( HexString ) )
	);
	// If the error checks did not return no errors, set
	// the JMP color to a value that will prodive the
	// calling program a key to what is wrong
	// A positive number indicates an error was found
	// A string of all 3's indicates a wrong lenght hex
	// was passed in
	// A string of 1's and 2's indicates that each position where
	// a 2 is returned, an invalid value was input
	If( Num( errVal ) != 111111,
		JMPColor = Num( errVal )
	);
	
	// Return the calculated value
	JMPColor;
);


jc = HexToJMPColor( "aa45af" );

If( jc < 0,
	New Window( "Example",
		Graph Box(
			Pen Color( "Black" );
			Pen Size( 2 );
			Fill Color( jc );
			Oval( 15, 75, 65, 55, 1 );
		)
	)
);