Jump to content

Hex To Mirc Color


err0r

Recommended Posts

For users trying to convert hex color codes to mirc color codes. This post from mirc official forum.

 

 

Try this:

Code:

hex2mirccolor {
  tokenize 46 $regsubex($1,/#?([a-f\d]{2})/gi,$base(\1,16,10) .)
  var %i = 0, %c, %d = 200000
  while %i < 16 {
    tokenize 32 $1-3 $replace($rgb($color(%i)),$chr(44),$chr(32))
    if $calc(($1 -$4)^2 + ($2 -$5)^2 + ($3 -$6)^2) < %d {
      %c = %i
      %d = $v1
    }
    inc %i
  }
  return %c
}

The above goes through all 16 colours and retrieves their RGB components in your current colour configuration (and thus does not make assumptions of what colour each number is - eg 3 might not be green in some colour themes). It then judges similarity between each colour in the list and the input colour and picks the one with the max similarity. In rather technical terms, similarity is judged by treating the compared colours as points in RGB space and computing their (squared) Euclidean distance. The return value is the index of the mirc colour that's most similar to the input.

 

Depending on how you judge similarity, this is probably not an ideal method. For example, it considers a very light green more similar to a very light red than to a very dark green. Most people would probably think the opposite. In this sense, a better way would be to work in the HSV (or HSL) colourspace and compare the hue of the input colour and each colour in mirc's colour list (and then optionally their lightness and saturation). If you want to go down that road, there are several RGB->HSL algorithms floating around which you could port to mirc, just google/wikipedia the respective terms.

 

If the above code doesn't work well enough for you and you're not comfortable with implementing the alternative I just described, I could give it try whenever I get the chance.

 

This post is from qwerty post is located here

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...