tglogo.png
0 LIKES

Snippet


Color RGB/HSL/HEX converter by Degausser

In Category General IRC Posted by Snippets On 12/06/23

A few functions to handle conversion between different types

$rgbtohex(N N N)
$rgbtohsl(N N N) .decimal
$hextorgb(N) .comma .single
$hextohsl(N) .decimal
$hsltorgb(N N N, [decimal]) .comma .single
$hsltohex(N N N, [decimal])

parameters dictate input, properties output:

decimal - signifies decimal format, rather than a percentage or degree with HSL input / output
single - returns single number representation of RGB values
comma - returns comma separated RGB values

spaces or commas can be used synonymously in the function call. Examples:

$rgbtohex(44 128 201) returns: #2C80C9
$rgbtohex(44,128,201) returns: #2C80C9
$rgbtohex(13205548) returns: #2C80C9

$hextorgb(#2C80C9) returns: 44 128 201
$hextorgb(2C80C9).decimal returns: 44,128,201
$hextorgb(2C80C9).single returns: 13205548

$rgbtohsl(44 128 201) returns: 207.9 64.1 48
$rgbtohsl(13205548) returns: 207.9 64.1 48
$rgbtohsl(44,128,201).decimal returns: 0.578 0.641 0.48

$hsltorgb(207.9 64.1 48) returns 44 128 201
$hsltorgb(207.9,64.1,48).comma returns 44,128,201
$hsltorgb(207.9,64.1,48).single returns 13205548
$hsltorgb(207.9 64.1 48).single returns 13205548
$hsltorgb(0.578 0.641 0.48 decimal) returns 44 128 201
$hsltorgb(0.578 0.641 0.48, decimal).single returns 13205548

$hextohsl(#2C80C9) returns: 207.9 64.1 48
$hextohsl(2C80C9).decimal returns: 0.578 0.641 0.48

$hsltohex(207.9 64.1 48) returns #2C80C9
$hsltohex(0.578 0.641 0.48 decimal) returns #2C80C9
rgbtohex {
  tokenize 44 $replace($1-3,$chr(32),$chr(44))
  if (!$2) tokenize 44 $rgb($1)
  return $+($chr(35),$base($1,10,16,2),$base($2,10,16,2),$base($3,10,16,2))
}

rgbtohsl {
  tokenize 44 $replace($1-3,$chr(32),$chr(44))
  if (!$2) tokenize 44 $rgb($1)
  var %r = $calc($1 / 255), %g = $calc($2 / 255), %b = $calc($3 / 255), %s = 0, %h = 0, %d
  var %sort = $sorttok(%r %g %b,32,n), %min = $gettok(%sort,1,32), %max = $gettok(%sort,3,32), %l = $calc((%max + %min) / 2)
  if (%min == %max) return 0 0 $iif($prop == decimal,$round(%l,3),$round($calc(%l * 100),1))
  %d = $calc(%max - %min)
  if (%max == %r) %h = $calc((%g - %b) / %d)
  elseif (%max == %g) %h = $calc(2 + ((%b - %r) / %d))
  else %h = $calc(4 + ((%r - %g) / %d))
  %h = $round($calc(%h * 60),1)
  if (%h < 0) inc %h 360
  %s = $iif(%l > 0.5,$calc(%d / (2 - %max - %min)),$calc(%d / (%max + %min)))
  if ($prop == decimal) return $round($calc((%h / 360)),3) $round(%s,3) $round(%l,3)
  return %h $round($calc(%s * 100),1) $round($calc(%l * 100),1)
}

hextorgb {
  var %h = $remove($1-,$chr(35)), %r = $rgb($base($mid(%h,1,2),16,10),$base($mid(%h,3,2),16,10),$base($mid(%h,5,2),16,10))
  if ($prop == single) return %r
  return $iif($prop != comma,$replace($rgb(%r),$chr(44),$chr(32)),$rgb(%r))
}

hextohsl {
  var %r = $hextorgb($1)
  return $iif($prop == decimal,$rgbtohsl(%r).decimal,$rgbtohsl(%r))
}

hsltorgb {
  tokenize 32 $replace($1-,$chr(44),$chr(32))
  if ($4 != decimal) tokenize 32 $calc($1 / 360) $calc($2 / 100) $calc($3 / 100)
  if ($2 == 0) var %r = $round($calc($3 * 255),0), %g = %r, %b = %r
  else {
    var %h = $1, %s = $2, %l = $3, %q, %p
    %q = $iif(%l < 0.5,$calc(%l * (1 + %s)),$calc((%l + %s) - (%l * %s)))
    %p = $calc((2 * %l) - %q)
    %r = $hue2rgb(%q, %p, $calc(%h + 1/3))
    %g = $hue2rgb(%q, %p, %h)
    %b = $hue2rgb(%q, %p, $calc(%h - 1/3))
    %r = $round($calc(%r * 255),0)
    %g = $round($calc(%g * 255),0)
    %b = $round($calc(%b * 255),0)
  }
  if ($prop == single) return $rgb(%r,%g,%b)
  return $iif($prop == comma,$+(%r,$chr(44),%g,$chr(44),%b),%r %g %b)
}

hsltohex {
  var %r = $hsltorgb($1-)
  return $rgbtohex(%r)
}

hue2rgb {
  var %t = $3
  if (%t < 0) inc %t
  if (%t > 1) dec %t
  if ($calc(6 * %t) < 1) return $calc($2 + (($1 - $2) * 6 * %t))
  if (%t < 0.5) return $1
  if ($calc(%t * 3) < 2) return $calc($2 + (($1 - $2) * 6 * (2/3 - %t)))
  return $2
}


Comments 0


Please note that on our website we use cookies necessary for the functioning of our website, cookies that optimize the performance. To learn more about our cookies, how we use them and their benefits, please read our Cookie Policy.
I Understand