Macro Express Forums

# Converting between colour codes?

## Recommended Posts

I'm sure we've discussed this before but so far I've not found it.

Does anyone have a macro for converting between MX's pixel code and RGB, the latter expressed either in its simple form or in hexadecimal format?

As an example, MX reports a pixel as 3749686 and a utility program I use reports it as RGB: R54 G55 B57 and also as RGB Color: 0x363739

Ideally I'd like two-way conversion, but I'd be happy if I could at least get from RGB (either format) to MX.

--

##### Share on other sites

I remember getting very in-depth with this and posting about it here. I just looked in my personal notes but don't see anything immediately. Perhaps if you limited your search to my posts? Also I think it was something where I was working out how to find fuzzy matches of a color for a progress indicator. Specifically one that was mostly green.

What I do remember is that the integer value is not really an integer. if is a conversion of the hex value. Pure white for instance is 0xFFFFFF and that converts to integer as 16777215. Also I think the hex value were backward. When I was trying to get near green colors I would convert the pixel integer value to hex, the extract the 3 hex values, convert back to integer, then use logic like Red and blue less than 50 and Green more than 200.

I looked but I can't lay my hands on that conversion routine quickly but I I'm pretty sure I remember how to do it. We know R01 G00 B00 = 0x010000 = 65536 and R00 G01 B00 = 0x000100 = 256. So it's a simple iterative reduction. Take a value EG 16777173 and fist divide by 65536 is 255 R65493. (remember R is the remainder) Now take the R65493 and divide by 265 which is 255 R213. So now there you have the 3 hex value in integer. 255 255 213.

Now your example value for your program are low so I can't tell if they are in hex or integer. EG the red value of 54 could be 0x36 or 0x54. But if the values they return for white are FF and not 255 255 255 but rather FF FF FF then we need to convert the integer of the component colors as well. Here simply divide a value, EG 213, by 16 and we get 13 R5. 13 in Hex is D and of course 5 in hex is 5. So in the example I gave of 16777173 the RGB values in hex were FF FF D5. Yup, a quick check of the Windows Calculator programmer mode proves this out.

If you would like some help writing some subroutine macros to do this I'd be happy to. Also there's probably a clever way to do it in a VBScript, something else I could help you with but it would probably be slower than doing the math in MEP.

##### Share on other sites

Do you need the conversion within the macro? When writing macros I do the color conversion manually. I use the Mouse Locator (click Tools, Launch Mouse Locator) to view both the integer and RGB (hex) value for the cursor underneath the cursor. As Cory mentions, the Windows Calculator in Programmer's mode allows conversion between hexadecimal and integer values.

On WIndows computers colors represented in hexadecimal format are often shown in BGR (Blue Green Red) rather than RGB (Red Green Blue). This is because Intel X86 and X86-64 processors are little-endian. The Macro Express Mouse Locator displays colors in RGB so you may need to switch the Red and Blue hexadecimal values.

You would expect numbers in RGB format to work like this:

E6 37 39 // more Red

36 E7 39 // more Green

36 37 E9 // more Blue

But it really works like this:

E6 37 39 // more Blue
36 E7 39 // more Green

36 37 E9 // more Red

##### Share on other sites

Thanks both, appreciate your help. I'll continue looking for a finished macro but failing that I might invest the effort and write one myself. Ideally I want to be able to enter the RGB, either via a user prompt or using a variable, and get the MX code displayed.

BTW, Ive always used a macro to display the Mouse Locator, essentially just launching C:\Program Files\Macro Express Pro\MSLocate.exe

But it never shows the RGB values, only the MX code. I'm guessing there's a program parameter that might do it, to give the same result as running it via Tools > Launch Mouse Locator.

If I change the macro so that it does it that way, it's always considerably slower.

--

##### Share on other sites

BTW, Ive always used a macro to display the Mouse Locator, essentially just launching C:\Program Files\Macro Express Pro\MSLocate.exe

But it never shows the RGB values, only the MX code. I'm guessing there's a program parameter that might do it, to give the same result as running it via Tools > Launch Mouse Locator.

The secret parameter is -hex.

Thanks Samrae!

--

##### Share on other sites

Now your example value for your program are low so I can't tell if they are in hex or integer. EG the red value of 54 could be 0x36 or 0x54.

Cory,

I gave the hex format too.

"...reports it as RGB: R54 G55 B57 and also as RGB Color: 0x363739".

The hex triplet is 36 37 39.

"Ideally I'd like two-way conversion, but I'd be happy if I could at least get from RGB (either format) to MX."

IOW from R54 G55 B57 (or RGB 0x363739) to 3749686

--

##### Share on other sites

Oh, IC. Sorry, missed that. Say, I think I remember now who the client was that I did that for but I don't see this macro anywhere in their files. But I noticed the file was ME3. Did you search the ME3 forums as well?

##### Share on other sites

Cory,

Yep! I scoured several hundred posts this morning about colour/color/conversion/hex/pixels/etc but didn't find a finished macro of the sort I described.

There were several relevant threads with posts from you, such as:

But if you did get around to writing one then AFAICS you never posted it in either ME3 or ME Pro forums.

--------------------

Here's my macro for the main conversion I need:

Script

// Converts a colour expressed in hex format to the integer used in MX.
// This version assumes manual entry but is easily changed to use passed variables.
Variable Set String %tHex%: Prompt // Enter the hex code.
// Remove prefix if necessary.
Variable Modify String %tHex%: Trim // Trim spurious characters on left or right.
Variable Modify String: Copy a substring in %tHex%, starting at 1 and 1 characters long to %tPrefix% // Extract first char, to test for #.
If Variable %tPrefix% Equals "#"
Variable Modify String %tHex%: Delete a substring starting at 1 and 1 characters long // Delete #.
End If
Variable Modify String: Copy a substring in %tHex%, starting at 1 and 2 characters long to %tPrefix% // Extract first 2 chars.
If Variable %tPrefix% Equals "0x"
Variable Modify String %tHex%: Delete a substring starting at 1 and 2 characters long // Delete first 2 chars.
End If
If Variable %tPrefix% Equals "he"
Variable Modify String %tHex%: Delete a substring starting at 1 and 3 characters long // Delete first 3 chars.
End If
// Now find the R, G and B components of that.
Variable Modify String: Copy a substring in %tHex%, starting at 1 and 2 characters long to %tR% // Extract R.
Variable Modify String: Copy a substring in %tHex%, starting at 3 and 2 characters long to %tG% // Extract G.
Variable Modify String: Copy a substring in %tHex%, starting at 5 and 2 characters long to %tB% // Extract B.
// Re-order those components from RGB to BGR.
Variable Modify String %tHexBGR%: Append Text String Variable (%tB%)
Variable Modify String %tHexBGR%: Append Text String Variable (%tG%)
Variable Modify String %tHexBGR%: Append Text String Variable (%tR%)

// Use the Calculator in Windows to convert that to decimal.
Activate or Launch: Window "Calculator", Program "calc.exe", Parameters ""
Wait for Window Title: Calculator
Text Type (Simulate Keystrokes): <F5> // Switch to Hex mode.
Delay: 0.1 seconds
Text Type (Simulate Keystrokes): %tHexBGR% // Enter hex BGR.
Delay: 0.1 seconds
Text Type (Simulate Keystrokes): <F6> // Switch to Decimal mode.
Delay: 0.1 seconds
Clipboard Copy
Variable Set Decimal %dResult% from the clipboard contents
Variable Modify Decimal %dResult%: Truncate to Integer (%nResult%)
Text Box Display: Result

Code

```<COMMENT Value="Converts a colour expressed in hex format to the integer used in MX."/>
<COMMENT Value="This version assumes manual entry but is easily changed to use passed variables."/>
<VARIABLE SET STRING Option="\x01" Destination="%tHex%" Prompt="Enter the colour in hex format. Case is ignored. Any of the following formats are acceptable: D4D0C8, 0xD4D0C8, #D4D0C8, hexD4D0C8" Mask="FALSE" OnTop="TRUE" Left="Center" Top="Center" Monitor="0" _COMMENT="Enter the hex code."/>
<COMMENT Value="Remove prefix if necessary."/>
<VARIABLE MODIFY STRING Option="\x00" Destination="%tHex%" _COMMENT="Trim spurious characters on left or right."/>
<VARIABLE MODIFY STRING Option="\x09" Destination="%tPrefix%" Variable="%tHex%" Start="1" Count="1" NoEmbeddedVars="FALSE" _COMMENT="Extract first char, to test for #."/>
<IF VARIABLE Variable="%tPrefix%" Condition="\x00" Value="#" IgnoreCase="TRUE"/>
<VARIABLE MODIFY STRING Option="\x0A" Destination="%tHex%" Start="1" Count="1" _COMMENT="Delete #."/>
<END IF/>
<VARIABLE MODIFY STRING Option="\x09" Destination="%tPrefix%" Variable="%tHex%" Start="1" Count="2" NoEmbeddedVars="FALSE" _COMMENT="Extract first 2 chars."/>
<IF VARIABLE Variable="%tPrefix%" Condition="\x00" Value="0x" IgnoreCase="TRUE"/>
<VARIABLE MODIFY STRING Option="\x0A" Destination="%tHex%" Start="1" Count="2" _COMMENT="Delete first 2 chars."/>
<END IF/>
<IF VARIABLE Variable="%tPrefix%" Condition="\x00" Value="he" IgnoreCase="TRUE"/>
<VARIABLE MODIFY STRING Option="\x0A" Destination="%tHex%" Start="1" Count="3" _COMMENT="Delete first 3 chars."/>
<END IF/>
<COMMENT Value="Now find the R, G and B components of that."/>
<VARIABLE MODIFY STRING Option="\x09" Destination="%tR%" Variable="%tHex%" Start="1" Count="2" NoEmbeddedVars="FALSE" _COMMENT="Extract R."/>
<VARIABLE MODIFY STRING Option="\x09" Destination="%tG%" Variable="%tHex%" Start="3" Count="2" NoEmbeddedVars="FALSE" _COMMENT="Extract G."/>
<VARIABLE MODIFY STRING Option="\x09" Destination="%tB%" Variable="%tHex%" Start="5" Count="2" NoEmbeddedVars="FALSE" _COMMENT="Extract B."/>
<COMMENT Value="Re-order those components from RGB to BGR."/>
<VARIABLE MODIFY STRING Option="\x07" Destination="%tHexBGR%" Variable="%tB%" NoEmbeddedVars="FALSE"/>
<VARIABLE MODIFY STRING Option="\x07" Destination="%tHexBGR%" Variable="%tG%" NoEmbeddedVars="FALSE"/>
<VARIABLE MODIFY STRING Option="\x07" Destination="%tHexBGR%" Variable="%tR%" NoEmbeddedVars="FALSE"/>
<COMMENT/>
<COMMENT Value="Use the Calculator in Windows to convert that to decimal."/>
<ACTIVATE OR LAUNCH Title="Calculator" Exact_Match="TRUE" Wildcards="FALSE" Path="C:\\WINDOWS\\system32\\calc.exe" Mode="\x00" Default_Path="TRUE" Wait="1" Wait_For_Program="12"/>
<WAIT FOR WINDOW TITLE Title="Calculator" Partial="FALSE" Wildcards="FALSE" Indefinite="TRUE" Hours="0" Minutes="0" Seconds="0"/>
<TEXT TYPE Action="0" Text="<F5>" _COMMENT="Switch to Hex mode."/>
<DELAY Flags="\x01" Time="0.1"/>
<TEXT TYPE Action="0" Text="%tHexBGR%" _COMMENT="Enter hex BGR."/>
<DELAY Flags="\x01" Time="0.1"/>
<TEXT TYPE Action="0" Text="<F6>" _COMMENT="Switch to Decimal mode."/>
<DELAY Flags="\x01" Time="0.1"/>
<CLIPBOARD COPY/>
<VARIABLE SET DECIMAL Option="\x02" Destination="%dResult%"/>
<VARIABLE MODIFY DECIMAL Option="\x06" Destination="%dResult%" Variable="%nResult%"/>
<TEXT BOX DISPLAY Title="Result" Content="{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang2057{\\fonttbl{\\f0\\fnil\\fcharset0 Tahoma;}{\\f1\\fnil Tahoma;}}\r\n\\viewkind4\\uc1\\pard\\qc\\b\\f0\\fs20 Colour in MX digital form = %nResult%\\b0\\f1\\fs16 \r\n\\par \\pard \r\n\\par }\r\n" Left="Center" Top="486" Width="278" Height="125" Monitor="0" OnTop="TRUE" Keep_Focus="TRUE" Mode="\x00" Delay="0"/>

```

Exported MEX macro attached

--

ColourHexToMX.mex

##### Share on other sites

• 3 weeks later...

I doubt I'll have the time to get around to this any time soon. One of my new year resolutions is to avoid fun but time consuming distractions that don't pay :-(

##### Share on other sites

Well, not sure why you would need to now?

##### Share on other sites

I doubt I'll have the time to get around to this any time soon. One of my new year resolutions is to avoid fun but time consuming distractions that don't pay :-(

I really do hope that's a smile at the end of your sentence and that you wrote in jest. Otherwise I just find it very sad.

##### Share on other sites

Ha! What's sad is somehow my entire message didn't make it into my post. Sorry about that. Now I don't remember what it was I wrote exactly but I'll try to explain.

I did have a converter I wrote for a client because it had a gradient green progress indicator so the exact color of green could not be determined. So I broke out the component colors and if they were greater than a given level of green and less than a given level of blue and red I considered it green. But I can't find the macro now and I know it took me a couple days to create and perfect. What I meant to relay is that I can't take even 2 hours off work at the moment to recreate that macro. The frown is an "I'm sorry I can't write this for you because I really want to". I like to help people but awhile back I got into the bad habit of spending 1- 2 hours a day helping people on this forum instead of helping my clients so I'm resisting getting too deep into any of these issues that might become time consuming as a meager effort at self-discipline.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.