It is currently April 25th, 2024, 12:12 pm

Extract separate RGB values regardless of color format (no Lua)

Tips and Tricks from the Rainmeter Community
User avatar
JelleDekkers
Posts: 127
Joined: September 27th, 2017, 6:32 pm
Location: Netherlands

Extract separate RGB values regardless of color format (no Lua)

Post by JelleDekkers »

So what this does is extract the separate R, G and B values from color codes, both decimal and hexadecimal. It's quite useful in case you need it. In the end, [mColorR],[mColorG] and [mColorB] will give you the separate values in decimal format.

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
Last edited by JelleDekkers on December 2nd, 2020, 9:07 am, edited 1 time in total.
User avatar
ikarus1969
Posts: 571
Joined: February 28th, 2011, 3:20 pm
Location: Vienna, Austria

Re: Extract separate RGB values regardless of color format (no Lua)

Post by ikarus1969 »

Thank you for reminding me of the [:xdigit:] character class in regular-expressions! :thumbup:

Edit: what site would you recommend when looking for regular-expression- learning? Currently i use http://www.regular-expressions.info/ but am always open for new sites regarding this.
User avatar
balala
Rainmeter Sage
Posts: 16168
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Extract separate RGB values regardless of color format (no Lua)

Post by balala »

JelleDekkers wrote: December 1st, 2020, 10:56 pm So what this does is extract the separate R, G and B values from color codes, both decimal and hexadecimal. It's quite useful in case you need it. In the end, [mColorR],[mColorG] and [mColorB] will give you the separate values in decimal format.
An intresting code, which can be extremely useful probably in many cases, however a few comments arrises (hope you don't mind):
  • A somehow similar code has been published long time ago (just to promote myself). There has been two codes, one for decimal to hexa conversion and another for the vice-versa conversion. However these codes don't do exactly the same thing as yours.
  • There is a small issue when extracting the R, G and B values from a decimal color code. In such a case the decimal code has to have exactly three digits on each component. For instance 255,240,0 (yellow) doesn't work, the code doesn't extract the components. It does for 255,240,000, but not for 255,240,0.
    There is an extremely simple solution to fix this: just replace all d{3} expressions in the Substitute options with d{1,3}.
  • Another thing you have to improve in your code is to modify it to can work even if the code has a transparency value as well, beside the color components. For instance it should work with a color code like 255,240,0,255 (or 255,240,000,255) as well.
User avatar
JelleDekkers
Posts: 127
Joined: September 27th, 2017, 6:32 pm
Location: Netherlands

Re: Extract separate RGB values regardless of color format (no Lua)

Post by JelleDekkers »

balala wrote: December 2nd, 2020, 7:18 am
  • There is a small issue when extracting the R, G and B values from a decimal color code. In such a case the decimal code has to have exactly three digits on each component. For instance 255,240,0 (yellow) doesn't work, the code doesn't extract the components. It does for 255,240,000, but not for 255,240,0.
    There is an extremely simple solution to fix this: just replace all d{3} expressions in the Substitute options with d{1,3}.
Ah, thanks for pointing that out, I'll change it right away.
User avatar
balala
Rainmeter Sage
Posts: 16168
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Extract separate RGB values regardless of color format (no Lua)

Post by balala »

JelleDekkers wrote: December 2nd, 2020, 9:04 am Ah, thanks for pointing that out, I'll change it right away.
Alright.
I still suggest to modify the code in away to work with alpha (transparency) values as well.