Lighten/Darken color

Get help with using AutoHotkey and its commands and hotkeys
think
Posts: 65
Joined: 09 Feb 2014, 05:20

Lighten/Darken color

15 Jun 2020, 15:46

I have searched the forum for the script which would lighten or darken the hex color without luck. Instead I found a javascript code here: https://stackoverflow.com/questions/5560248/programmatically-lighten-or-darken-a-hex-color-or-rgb-and-blend-colors
Can anyone suggest how to change this into AutoHotkey? Thanks!

Code: Select all

function LightenDarkenColor(col, amt) {
  var num = parseInt(col, 16);
  var r = (num >> 16) + amt;
  var b = ((num >> 8) & 0x00FF) + amt;
  var g = (num & 0x0000FF) + amt;
  var newColor = g | (b << 8) | (r << 16);
  return newColor.toString(16);
}

// TEST
console.log(LightenDarkenColor("3F6D2A", -40));
User avatar
boiler
Posts: 5572
Joined: 21 Dec 2014, 02:44

Re: Lighten/Darken color

15 Jun 2020, 15:54

Code: Select all

MsgBox, % Format("{:X}", LightenDarkenColor("3F6D2A", -40))
return

LightenDarkenColor(col, amt) {
	col := "0x" . col
	r := (col >> 16) + amt
	b := ((col >> 8) & 0x00FF) + amt
	g := (col & 0x0000FF) + amt
	newColor := g | (b << 8) | (r << 16)
	return newColor
}
think
Posts: 65
Joined: 09 Feb 2014, 05:20

Re: Lighten/Darken color

15 Jun 2020, 16:17

Thank you for such a fast reply. It works. :)
There are some limitations though, I was not able to enter amounts >40.

Code: Select all

color:="2d78d6"
amts:="-40,-20,0,20,40"
loop, parse, amts, `,
{
	ncolor:=Format("{:X}", LightenDarkenColor(color, A_LoopField))
	gui, add, progress, w50 h50 background%ncolor%
}	
gui, show

LightenDarkenColor(col, amt) {
	col := "0x" . col
	r := (col >> 16) + amt
	b := ((col >> 8) & 0x00FF) + amt
	g := (col & 0x0000FF) + amt
	newColor := g | (b << 8) | (r << 16)
	return newColor
}
User avatar
boiler
Posts: 5572
Joined: 21 Dec 2014, 02:44

Re: Lighten/Darken color

15 Jun 2020, 16:27

There should be no limitations other than trying to lower a component below 0 or higher than 0xFF, the same "limitation" the original has. One of the components in your first example is only 42 (0x2A) and only 45 (0x2D) in your latest one, so you can't lower it by more than that without that problem. And 0xD6 is 214, so you can't increase it by much more than 40 before you go over 255 (0xFF).

Some error checking could be added to it to make sure you don't ask for something that doesn't make sense.

Return to “Ask For Help”

Who is online

Users browsing this forum: 4k3or3et, Bing [Bot], blenderfish, coder_chick, DaveT1, hackkk745, RubbeH, TheDewd and 44 guests