Page 1 of 2

Color conversion without lua

Posted: August 11th, 2022, 2:28 pm
by mkboynton
I have a measure that extracts the DWM color from the registry.

Code: Select all

[mDWMCOLORFromRegistry]
Measure=Registry
RegHKey=HKEY_CURRENT_USER
RegKey=Software\Microsoft\Windows\DWM
RegValue=AccentColor
DynamicVariables=1
It returns a value like this : 4284168218 as both a number and a string. How can I convert it to a color code like this : 26,56,91,196 ?


I found this code by JelleDekkers but it is returning these numbers : mColorR = 66, mColorG = 132, mColorB = 22 which do not match the color code from the SysColor plugin which is 26,56,91,196.
Note: The color returned by the SysColor plugin matches my current accent color.

Code: Select all

[Variables]
Color=
; Either RRGGBB or RRR, GGG, BBB.

ColorFormulaPrefixDec=
ColorFormulaPrefixHex=0x
; Don't touch these two! They're needed for the hexadecimal to decimal conversion.

; ================================================

[mColorInputType]
Measure=String
String=#Color#

RegExpSubstitute=1
Substitute="\d+ *, *\d+ *, *\d+.*":"Dec", "[[:xdigit:]]{6}.*":"Hex"

UpdateDivider=-1

[mColorRRAW]
Measure=String
String=#Color#

RegExpSubstitute=1
Substitute="(\d+) *, *(\d+) *, *(\d+).*":"\1", "([[:xdigit:]]{2})([[:xdigit:]]{2})([[:xdigit:]]{2}).*":"\1"

UpdateDivider=-1

[mColorR]
Measure=Calc
Formula=[#ColorFormulaPrefix[&mColorInputType]][#CurrentSection#RAW]

UpdateDivider=-1
DynamicVariables=1

[mColorGRAW]
Measure=String
String=#Color#

RegExpSubstitute=1
Substitute="(\d+) *, *(\d+) *, *(\d+).*":"\2", "([[:xdigit:]]{2})([[:xdigit:]]{2})([[:xdigit:]]{2}).*":"\2"

UpdateDivider=-1

[mColorG]
Measure=Calc
Formula=[#ColorFormulaPrefix[&mColorInputType]][#CurrentSection#RAW]

UpdateDivider=-1
DynamicVariables=1

[mColorBRAW]
Measure=String
String=#Color#

RegExpSubstitute=1
Substitute="(\d+) *, *(\d+) *, *(\d+).*":"\3", "([[:xdigit:]]{2})([[:xdigit:]]{2})([[:xdigit:]]{2}).*":"\3"

UpdateDivider=-1

[mColorB]
Measure=Calc
Formula=[#ColorFormulaPrefix[&mColorInputType]][#CurrentSection#RAW]

UpdateDivider=-1
DynamicVariables=1

Re: Color conversion without lua

Posted: August 11th, 2022, 3:15 pm
by SilverAzide
mkboynton wrote: August 11th, 2022, 2:28 pm I have a measure that extracts the DWM color from the registry.
[snip]
It returns a value like this : 4284168218 as both a number and a string. How can I convert it to a color code like this : 26,56,91,196 ?


I found this code by JelleDekkers but it is returning these numbers : mColorR = 66, mColorG = 132, mColorB = 22 which do not match the color code from the SysColor plugin which is 26,56,91,196.
Note: The color returned by the SysColor plugin matches my current accent color.
I'm not sure why you'd want to handicap yourself by specifying "without Lua". You are going to have a tough time without it. JelleDekkers code is not totally applicable to your issue without some additional work.

The problem is that there is no built-in Rainmeter function that will convert a number to a hexadecimal value. You can easily use a one-line Lua command to do it: string.format("%x", num). You can use simple inline Lua to do this (see the docs on this topic).

Once you have the hex value, then JelleDekker's code will work, as long as you do some modifications first.

Here's the background, and you can confirm this with the Windows Calculator set to Programmer mode.

The decimal number 4284168218 converts to hexadecimal number "FF5B381A". If you look at this carefully, the string is in the form "AABBGGRR". In other words (going from right to left), "1A" = 26, "38" = 56, "5B" = 91, "FF" is 255 (your alpha value of 196 is not correct for some reason). So this is the data you need.

Once you have this hex value, you'll then need to write some regexp substitution code to swap the order of the hex string values to "RRGGBBAA" format, THEN JelleDekkers code will work.

To do this without any Lua will require you to use bit-shifting operations to pull out the R, G, B, A values from the number to assemble it into "R, G, B, A" format. Rainmeter doesn't have bit-shifting functions, so workarounds are needed (bitmasking using bitwise operators and division to do the shift). You really want to go down that road?

Re: Color conversion without lua

Posted: August 11th, 2022, 4:40 pm
by mkboynton
My problem is that I am trying to create a theme for Droptop Four by Cariboudjan without using a 3rd party plugin (SysColor) and I can't use an external lua script either. The theme is comprised of two files that are a bunch of variables to tell Droptop what color to assign everything. I can add measures, but that's about it. The registry looks like it contains the hexadecimal value as well, tha Data column in Registry Editor shows 0xff5b381a (4284168218). Is there a way in my measure to force it to retrieve the hexadecimal value?

Re: Color conversion without lua

Posted: August 11th, 2022, 7:26 pm
by SilverAzide
mkboynton wrote: August 11th, 2022, 4:40 pm My problem is that I am trying to create a theme for Droptop Four by Cariboudjan without using a 3rd party plugin (SysColor) and I can't use an external lua script either. The theme is comprised of two files that are a bunch of variables to tell Droptop what color to assign everything. I can add measures, but that's about it. The registry looks like it contains the hexadecimal value as well, tha Data column in Registry Editor shows 0xff5b381a (4284168218). Is there a way in my measure to force it to retrieve the hexadecimal value?
Nope. There is no hexadecimal value; it's like in The Matrix, "there is no spoon". What you see in the registry editor is displayed for your convenience. The value is a DWORD (32-bit unsigned integer), so it is a number. Hexadecimals aren't really used by computers, they are more like a way for people to more easily see/understand data arranged in bytes.

So why can't you use Lua? If you can add measures, you can definitely add Lua. And if you can add Lua, you can do anything you want. Don't forget you can use bangs in Lua to change variables, meters, measures and anything else you can do with a Rainmeter bang.

Re: Color conversion without lua

Posted: August 11th, 2022, 7:59 pm
by balala
mkboynton wrote: August 11th, 2022, 2:28 pm I have a measure that extracts the DWM color from the registry.

Code: Select all

[mDWMCOLORFromRegistry]
Measure=Registry
RegHKey=HKEY_CURRENT_USER
RegKey=Software\Microsoft\Windows\DWM
RegValue=AccentColor
DynamicVariables=1
It returns a value like this : 4284168218 as both a number and a string. How can I convert it to a color code like this : 26,56,91,196 ?
A few years ago I wrote two codes for converting decimal color codes to hexa and another to convert hexa codes to decimal codes (see the codes in the posted links). These codes don't use lua scripts.
However the [mDWMCOLORFromRegistry] measure is returning the color code in its decimal form, but this is not the format we're using in Rainmeter, with the R,G,B,A values. It most probably can be converted but this requires some additional work and additional measures to be added to the code.
If you are interested, in the upcoming days I may try to write the code as you need it. So are you interested, or SilverAzide found in meantime a proper solution?

Re: Color conversion without lua

Posted: August 11th, 2022, 8:31 pm
by mkboynton
All of this decimal and hexadecimal stuff is like magic to me. So yes, if you have the time I would appreciate any help I can get. The creator of Droptop has some kind of script that generates .rmskin files so you can share your theme with others, it only includes the two files for the them and any special fonts you might use. It will not add additional files such as a lua script that is external.

Re: Color conversion without lua

Posted: August 12th, 2022, 6:01 am
by nek
It seems that there are several color codes in the Windows Registry.

Code: Select all

 [HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\DWM]
 "AccentColor"=dword:ffd77800        >> alpha?-blue-green-red  ** It seems that the alpha value has only ff(255). **
 "ColorizationColor"=dword:c40078d7  >> alpha-red-green-blue
 [HKEY_CURRENT_USER\Control Panel\Colors]
 "Background"="229 229 229"          >> red-green-blue
You can try this code.
#1

Code: Select all

[Rainmeter]
Update=-1
;DefaultUpdateDivider=-1
AccurateText=1

[sAccentColor]
Measure=Registry
RegHKey=HKEY_CURRENT_USER
RegKey=SOFTWARE\Microsoft\Windows\DWM
RegValue=AccentColor

[sRed]
Measure=Calc
Formula=sAccentColor&255

[sGreen]
Measure=Calc
Formula=(sAccentColor>>8)&255

[sBlue]
Measure=Calc
Formula=(sAccentColor>>16)&255

;[sAlpha]
;Measure=Calc
;Formula=(sAccentColor>>24)&255

[Square]
Meter=Image
W=100
H=100
;SolidColor=[sRed],[sGreen],[sBlue],[sAlpha]
SolidColor=[sRed],[sGreen],[sBlue]
DynamicVariables=1
#2

Code: Select all

[Rainmeter]
Update=-1
;DefaultUpdateDivider=-1
AccurateText=1

[sAccentColor]
Measure=Registry
RegHKey=HKEY_CURRENT_USER
RegKey=SOFTWARE\Microsoft\Windows\DWM
RegValue=AccentColor
;OnUpdateAction=[!SetOption Square SolidColor "([#CURRENTSECTION#:]&255),([#CURRENTSECTION#:]>>8&255),([#CURRENTSECTION#:]>>16&255),([#CURRENTSECTION#:]>>24&255)"][!UpdateMeter Square][!DisableMeasure #CURRENTSECTION#]
OnUpdateAction=[!SetOption Square SolidColor "([#CURRENTSECTION#:]&255),([#CURRENTSECTION#:]>>8&255),([#CURRENTSECTION#:]>>16&255)"][!UpdateMeter Square][!DisableMeasure #CURRENTSECTION#]

[Square]
Meter=Image
W=100
H=100
;SolidColor=255,255,255,255
SolidColor=255,255,255
Edited. 2022-08-19
Changed &0xFF to &255 in the code.
Reason: The following number notation will be convert to data type long (4 bytes) once, then evaluate the formula.
I thought that 255 (data type double , 8 bytes) is better for coding.
Rainmeter Docs wrote:0b - Binary number (base 2) (ex: 0b110110 - returns 54 in decimal)
0o - Octal number (base 8) (ex: 0o123 - returns 83 in decimal)
0x - Hexadecimal number (base 16) (ex: 0xF1 - returns 241 in decimal)

I just found out, Rainmeter has bitwise shift operators << >>
📗 Formula Syntax 📜 MathParser.cpp 📘 C++ operator precedence, Data Type Ranges

Re: Color conversion without lua

Posted: August 12th, 2022, 12:15 pm
by SilverAzide
nek wrote: August 12th, 2022, 6:01 am I just found out, Rainmeter has bitwise shift operators << >>
Nice find! This is not mentioned in the user manual docs.

Re: Color conversion without lua

Posted: August 12th, 2022, 1:57 pm
by mkboynton
nek you are a genius, you make this old man look like he needs to go back to school. That works perfectly and looks so simple, but I never would have figured that out without your help. Thanks a million, you just saved my project.

Re: Color conversion without lua

Posted: August 12th, 2022, 2:51 pm
by nek
Glad to help.

([#CURRENTSECTION#:]>>16&255). This code is to retrieve blue value and some description here.

For example 4292311040 >>16 &255
bitwise_op.png
Note) Bitwise operators << >> are undocumented in the Rainmeter Docs.
The code may not work in the future and bitwise shift operator may not work in some cases. Maybe we should NOT ask the developers about the bitwise shift operators.