Calculating visually complementing hexadecimal colors

Forum: ProgrammersTotal Replies: 4
Author Content
dave

Jul 28, 2003
11:10 AM
I hope I didn't scare anyone away with the subject of this thread. :)

Let's say that I give a form that accepts a hexadecimal value for a color, and the user enters in "FFFFFF" (for white).

Then, I'd like the server to calculate the complementing color that might be used as a foreground color on top of the background color that the user gave me. In this example, the complenting color would be "000000" (black). Black text on white background - that's a nice complement.

Now what if the user enters something a bit more complicated, like say an odd shade of red like #990000. From experiments I have conducted, I can take that hex value and convert it to an RGB decimal value. Then, reverse each RGB (add 128 if less than 128, subtract 128 if more than 128) and I get something that is "okay" but not perfect. Using that method, from red's #990000, I get blue's #181083. A reverse to be sure, but not exactly -complementing- of it. I think white or black makes a more suitable complement.

This is hard to describe, so I wrote a little script to demonstrate exactly what I am trying to do. Please see it in action at this URL: [HYPERLINK@standardout.com]

You can see that it doesn't produce a perfect color every time. I suspect the key is my subtracting or adding 128 to the red,green,blue values, but I'm not sure. 128 might not be the correct number.

Short of actually writing special cases for each of the color possibilities, does anyone have a good algorythm for calculating nice and attractive complementing colors?

Dave
dave

Jul 28, 2003
1:12 PM
Since posting the above this morning, I'd done quite a bit more research and experimentation. I have updated the code at [HYPERLINK@standardout.com] to reflect my latest work. I think it is working pretty well, but on some colors I am not 100% satisfied.

What I am doing now is completely flipping all 6 individual hexadecimal numbers, and adding 1 to the lowest of those numbers. (I don't know why, but this is the algorythm I found on a research paper on this subject by NASA).

Anyway, the code is above. Scroll down to the two functions are the bottom to see the main part of the code. It does produce readable text on just about every background selected, so I am happy.

Dave
coderfrommaine

Jul 28, 2003
5:10 PM
Neat!
J_Maxwell

Aug 04, 2003
11:53 AM
Hey.

That was some really neat code! Would it be okay for me to copy it, and use it in my shopping cart?

Thanks,

J_Maxwell
dave

Aug 04, 2003
4:00 PM
Sure thing - have at it!

Posting in this forum is limited to members of the group: SITEADMINS, SUBSCRIBERS, MEMBERS.]

the Open Forums!

  Login
If you don't have an account yet, visit the registration page to sign up.

If you already have an account, you may login here:

Username: Password:

  Welcome to the Open Forums!!
Welcome to the Open Forums!!

  Hosted By...

This website is hosted by:

 -
PreparingSons
 - Titus2.com


[ Copyright © the Open Forums! | All times are recorded in ET ]

[ Contact Us ]

Login

Powered by Scif 5.3 build 285 by StandardOut, Inc.