Skip to content
MSN Addons

hex-to-rgb color value converter

A TG007 community discussion started by vexation.

Closed discussion 3 posts
Page 1 of 1
Closed
Topic #529 · 3 published posts · 600 views

well i accidentally figured this out.. i have been looking for such a thing for a while so maybe it will help somebody else. it takes a Hex color value (e.g. FF66CC) and turns it into an RGB value (e.g. 255,102,204). Useful with things like the colored welcome messages in your chatrooms.

alias unhex {
 var %l = 1, %r
 while (%l <= $len($1-)) {
   if (!$2) {
     if ($mid($1-,%l,2) != 20) %r = %r $+ $chr($base($mid($1-,%l,2),16,10))
     else %r = %r $chr($base($mid($1-,%l,2),16,10))
   }
   else %r = %r $+ $chr($base($mid($1-,%l,2),16,10))
   inc %l 2
 }
 return %r
}
alias h2r {
 var %h = $replace($1,00,zz)
 var %c = 1
 while ($right($left(%h,$calc(%c * 2)),2) != $null) {
   var %m = $ifmatch
   if (%m == zz) var %r = %r 0
   else var %r = %r $asc($unhex(%m))
   inc %c 1
   if (%c = 4) break
 }
 return $replace(%r,$chr(32),$chr(44))
}

$h2r(FF66CC) returns 255,102,204

also, the reverse:

alias tohex {
 var %l = 1, %r
 while (%l <= $len($1-)) {
   %r = %r $+ $base($asc($mid($1-,%l,1)),10,16,2)
   inc %l
 }
 return %r
}
alias r2h {
 :r2hok
 if ($3) {
   var %h = $tohex($chr($1))
   var %h = %h $+ $tohex($chr($2))
   var %h = %h $+ $tohex($chr($3))
 }
 else { tokenize 44 $1 | goto r2hok }
 return $upper(%h)
}

$r2h(255,102,204) returns FF66CC

this works if you type in the 3 RGB values with commas or if you refer to an RGB value with a variable or identifier, either way.

 

*edit - i got the $unhex() and $tohex() aliases from Vincula smile.gif (thanks exo)

Edited May 24, 2004 4:56 AM by vexation

bfush.PNG

in my opion it is a very useful addon for people who use emoticons. I personally needed something like this about 2 months ago, but i found a way around it, so yea, but if i had this addon about 2 months ago, i would have definatly used it. good job.

there is a small program on techgear called color cop , that does this and more lol

also please odnt start new threads when there is one for addons allready

Edited May 24, 2004 11:30 AM by ozzy

My code does NOT have bugs. It just develops random features.