RGB triplet to hex triplet converter

By Teh Maestro on Mar 21, 2007

What it does: This script converts an RGB triplet (the red, blue, and green values of a color, and example would be 43,126,230) into a hex triplet (such as 2b7ee6.) I do acknowledge the fact that using $base($rgb($3,$2,$1),10,16) would have allowed me to cut 8 lines from the script, but I didn't want to use that.

To use: $hex(R,G,B), where R, G, and B are between 0 and 255.
I.E. //echo -a $hex(43,126,230) would echo to the active channel: 2b7ee6.

;##########################################
;# Teh Maestro - http://www.fpsbanana.com #
;# #fpsbanana on irc.GameSurge.net        #
;##########################################
;Usage: $hex(0-255,0-255,0-255)

alias hex {
  if ($3) || ($3 == 0) {
    if ($1 isnum 0-255) && ($2 isnum 0-255)  && ($3 isnum 0-255) {
      var %h1 = $1 + 16, %h2 = $2 + 16, %h3 = $3 + 16, %g = 1, %d = $null, %j = 0 1 2 3 4 5 6 7 8 9 a b c d e f
      while (%g <= 3) {
        var %f = $+(%,h,%g)
        var %k = $floor( $calc( %f / 16 ) ), %l = $iif( %f == 16,1,$eval( $calc( ( %f % 16 ) + 1 ) ) )
        var %d = $+(%d,$gettok(%j,%k,32),$gettok(%j,%l,32))
        inc %g
      }
      return %d
      halt
    }
    else {
      echo $color(info) -m * /hex: parameters must be integers between 0 and 255
      halt
    }
  }
  else {
    echo $color(info) -m * /hex: insufficient parameters
    halt
  }
}

Comments

Sign in to comment.
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.