It is currently September 21st, 2019, 5:36 am

## Change Color (Gradient) based on value

Help with creating, editing & fixing problems with skins
balala
Rainmeter Sage
Posts: 8631
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

### Re: Change Color (Gradient) based on value

Carje wrote:
March 22nd, 2019, 8:47 pm
Perfect, it worked!

So, to understand the code:
R = Clamp((5.1*[MeasureCPU]),0,255) -> R is 5.1 * MeasureCPU value with a value of min 0 and max 255
G = Clamp((255-(2.55*[MeasureCPU])),0,255) -> G is 2.55 * MeasureCPU value, subtracted from 255, with a value of min 0 and max 255
B = 0
MeasureCPU value being an integer of CPU usage between 0 and 100

Is this right ?
It is, perfectly.
To explain why have I done it this way, you probably know that green is 0,255,0, red is 255,0,0, while orange is 255,127,0. So, from green to orange (from 0% up to 50%), the red component of the color code has to increase from 0 up to 255 and above 50% it has to be kept on 255. Multiplying the CPU usage percent (the value of the [MeasureCPU] measure) with 5.1, I got exactly this variation. Since the product above 50% would increase above 255 (which shouldn't have to go on), I used the Clamp function, to limit the result.
The green component has to decrease from 255 (the green component of the color used for 0%), to 0% (the same green component for the color used on 100%). The result of multiplication of 2.55 with the CPU usage increases from 0 to 255. I substracted this value from 255. Just for safe I also added another Clamp function.
I suppose is clare what have I done for the blue component: I kept it on 0, since it's not involved at all in non of the needed colors.
I'm glad if it does work as you wanted.
Carje
Posts: 6
Joined: March 22nd, 2019, 4:23 pm

### Re: Change Color (Gradient) based on value

balala wrote:
March 22nd, 2019, 9:01 pm
It is, perfectly.
To explain why have I done it this way, you probably know that green is 0,255,0, red is 255,0,0, while orange is 255,127,0. So, from green to orange (from 0% up to 50%), the red component of the color code has to increase from 0 up to 255 and above 50% it has to be kept on 255. Multiplying the CPU usage percent (the value of the [MeasureCPU] measure) with 5.1, I got exactly this variation. Since the product above 50% would increase above 255 (which shouldn't have to go on), I used the Clamp function, to limit the result.
The green component has to decrease from 255 (the green component of the color used for 0%), to 0% (the same green component for the color used on 100%). The result of multiplication of 2.55 with the CPU usage increases from 0 to 255. I substracted this value from 255. Just for safe I also added another Clamp function.
I suppose is clare what have I done for the blue component: I kept it on 0, since it's not involved at all in non of the needed colors.
I'm glad if it does work as you wanted.
I was wondering how you got those multipliers, I suppose you just tested till it worked
It does work as intended, thank you!
balala
Rainmeter Sage
Posts: 8631
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

### Re: Change Color (Gradient) based on value

Carje wrote:
March 22nd, 2019, 9:18 pm
I was wondering how you got those multipliers, I suppose you just tested till it worked
No, at all. I calculated them with a simple division. For example the green component has to vary between 255 and 0, while the percentage goes from 0 to 100. This means for every percent, the color component has to increase with 2.55, given by 255/100.
Carje wrote:
March 22nd, 2019, 9:18 pm
It does work as intended, thank you!