tags:

views:

19

answers:

4

Hi, I'm putting in a function which will allow a user to input a color (eg: purple) and it will change the look of their profile to be purple. It's interpreted from text into a 'Color' class which stores them inside itself as RGB numbers (int for red, one for green and other for blue). What i don't know how to do is logically turn these three numbers into another 3 which will make a readable colour.

Can anyone help me on how to do this?

Joe

A: 

Obviously, there's not a name for every possible RGB combination! Presumably you want to find a nearby combination that you have specified a name for?

So really all you need is a way of defining how "close" one RGB is to another. For simplicity, I would suggest Euclidean-distance-squared, i.e. (R2-R1)^2 + (G2-G1)^2 + (B2-B1)^2. Then all you need to do is iterate through all your "named" colours, and find the one with the smallest distance.

Oli Charlesworth
@Joe Simpson: Oh hang on, by "readable" do you mean "legible", as in "I want to generate a legible foreground colour given an arbitrary background colour"?
Oli Charlesworth
So it's readable yeah
Joe Simpson
A: 

You could look at the rgb.txt file that comes with X11.

A parser for that could provide a translation between those strings and the RGB values wouldn't be terribly hard to build and would likely do what you're looking for.

Arcane
I'm sorry but my parser can already pick up many names and methods for colours
Joe Simpson
@Joe what is your question then, exactly? Can you elaborate?
Pekka
I'm trying to turn the RGB values i already parsed into a readable colour
Joe Simpson
A: 

As others already say, there are 16.7 million possible combinations, all of which obviously can't have a defined name.

You're not going into detail about your use case, but if you want to make it end user friendly, how about using percentages?

80% red, 50% green, 23% blue

this is perfectly understandable for a non-technical person as well. You would limit (from 256^3 to 100^3) the number of possibilities if you use integer percentage values, but not as much as confining the user to a fixed palette of named colours.

Pekka
I'm going to add a javascript colour picker when javascript is available
Joe Simpson
+1  A: 

If I'm interpreting your question correctly, you're looking for a readable text color after someone has chosen a background theme color. This was answered in an older question:

http://stackoverflow.com/questions/946544/good-text-foreground-color-for-a-given-background-color

Mark Ransom