HEX Color luminance converter, function request

Discuss and share scripts and script files...
Post Reply
jupe
Posts: 1126
Joined: 20 Oct 2017 21:14
Location: 1903 x64

HEX Color luminance converter, function request

Post by jupe » 10 Feb 2019 04:31

My math skills being severely lacking and not having much experience with javascript or hex conversion, I was wondering if someone could help me convert the below javascript function to an XY function, or if someone already has a similar or better way of doing it (lighten/darken hex colors) in XY I would appreciate it.

Code: Select all

function ColorLuminance(hex, lum) {

	// validate hex string
	hex = String(hex).replace(/[^0-9a-f]/gi, '');
	if (hex.length < 6) {
		hex = hex[0]+hex[0]+hex[1]+hex[1]+hex[2]+hex[2];
	}
	lum = lum || 0;

	// convert to decimal and change luminosity
	var rgb = "#", c, i;
	for (i = 0; i < 3; i++) {
		c = parseInt(hex.substr(i*2,2), 16);
		c = Math.round(Math.min(Math.max(0, c + (c * lum)), 255)).toString(16);
		rgb += ("00"+c).substr(c.length);
	}

	return rgb;
}

SammaySarkar
Posts: 4190
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

Re: HEX Color luminance converter, function request

Post by SammaySarkar » 10 Feb 2019 06:20

Basically this.

Code: Select all

NAMESPACE color
FUNCTION luminance($hex, $lum=0) {
  // validate hex color
  $hex = regexreplace($hex, '^#', '');
  assert (strlen($hex) == 6) || (strlen($hex) == 3) || regexmatches($hex, '[^0-9a-f]'), "incorrect hex color: $hex";
  $hex = (strlen($hex) == 3) ? strrepeat(substr($hex, 0, 1), 2) . strrepeat(substr($hex, 1, 1), 2) . strrepeat(substr($hex, 2, 1), 2) : $hex;
  // apply luminance
  $result = '#'; $i = 0;
  while ($i++ < 3) {
    $color = hextodec(substr($hex, 2*($i-1), 2));
    $color = dectohex(round(math::min(math::max($color + ($color * $lum), 0), 255)));
    $result .= substr("00" . $color, -2);
  }
  return $result;
}
NAMESPACE math
FUNCTION max($x, $y) { return ($x >= $y) ? $x : $y; }
FUNCTION min($x, $y) { return ($x <= $y) ? $x : $y; }

"test"
   echo color::luminance("#69c", 0);		// returns "#6699cc"
   echo color::luminance("6699CC", 0.2);	// "#7ab8f5" - 20% lighter
   echo color::luminance("69C", -0.5);	        // "#334d66" - 50% darker
   echo color::luminance("000", 1);		// "#000000" - true black cannot be made lighter!
[edited: reduce LOC. fix small miscalculation]

SammaySarkar
Posts: 4190
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612 / Dhaka
Contact:

Re: HEX Color luminance converter, function request

Post by SammaySarkar » 10 Feb 2019 06:51

I used the builtin undocumented dectohex() function there. You can also use this UDF version instead. Same result.

jupe
Posts: 1126
Joined: 20 Oct 2017 21:14
Location: 1903 x64

Re: HEX Color luminance converter, function request

Post by jupe » 10 Feb 2019 08:55

Awesome, thanks SammaySarkar!

I slapped together a very quick (and rough) demo script if anyone is interested:

Code: Select all

	$input = trim(input(,,'6699CC'), '[#]');
	$dir   = <get rs 1 +->;
	$divs  = '<div style="background:#' . $input . '; height:10%">#' . $input . '</div>';
	while ($i++ < 9) {
		$cs = color::luminance($input, $dir . "0." . $i);
		$divs .= '<div style="background:' . $cs . '; height:10%">' . $cs . '</div>';
	}
	html("<html><body>$divs</body></html>");


Post Reply