Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2005

Tip: Looking for answers? Try searching our database.

Constrasting Color

Thread view: 
- - 10 May 2005 15:04 GMT
If I have a Color object, is it possible to get a constrasting Color in
return?

Black: 255 255 255 becomes White: 0 0 0 so on and so forth...
Arnaud Berger - 10 May 2005 15:25 GMT
Hi,

What about this :

Color originalColor=....;

int red=originalColor.getRed();
int green=originalColor.getGreen();
int blue=originalColor.getBlue();

newRed=255-red;
newGreen=255-green;
newBlue=255-blue;

Color newColor=new Color(newRed,newGreen,newBlue);

Regards,

Arnaud

> If I have a Color object, is it possible to get a constrasting Color in
> return?
>
> Black: 255 255 255 becomes White: 0 0 0 so on and so forth...
Anton Spaans - 10 May 2005 15:30 GMT
> Hi,
>
[quoted text clipped - 11 lines]
>
> Color newColor=new Color(newRed,newGreen,newBlue);

This won't work with the color gray........
-- Anton.

> Regards,
>
[quoted text clipped - 4 lines]
> >
> > Black: 255 255 255 becomes White: 0 0 0 so on and so forth...
Chris Smith - 10 May 2005 15:39 GMT
> > Color originalColor=....;
> >
[quoted text clipped - 9 lines]
>
> This won't work with the color gray........

Okay, so for the maximum difference:

Color originalColor = ....;

int red = originalColor.getRed();
int green = originalColor.getGreen();
int blue = originalColor.getBlue();

newRed = (red < 128) ? 255 : 0;
newGreen = (green < 128) ? 255 : 0;
newBlue = (blue < 128) ? 255 : 0;

Color newColor = new Color(newRed, newGreen, newBlue);

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

shakah - 10 May 2005 18:15 GMT
How about converting the color to HSV and jumping to the other side of
the color wheel? I don't think it'll work for greys (including white
and black), though...

 java.awt.Color cOrig = ...
 float [] aHSB = java.awt.Color.RGBtoHSB(
     cOrig.getRed()
    ,cOrig.getGreen()
    ,cOrig.getBlue()
    ,null
    ) ;
 java.awt.Color cCompl = java.awt.Color.getHSBColor(
     aHSB[0] > 0.5f ? aHSB[0] - 0.5f : aHSB[0] + 0.5f
    ,aHSB[1]
    ,aHSB[2]
    ) ;

?

> If I have a Color object, is it possible to get a constrasting Color in
> return?
>
> Black: 255 255 255 becomes White: 0 0 0 so on and so forth...
Marcin Grunwald - 11 May 2005 11:18 GMT
> If I have a Color object, is it possible to get a constrasting Color in
> return?
>
> Black: 255 255 255 becomes White: 0 0 0 so on and so forth...

Maybe use xor function with 255 value:

For example:
0 ^ 255 = 255;
255 ^ 255 = 0;

--
Cheers
grundig
Eric Sosman - 11 May 2005 15:27 GMT
>>If I have a Color object, is it possible to get a constrasting Color in
>>return?
[quoted text clipped - 6 lines]
> 0 ^ 255 = 255;
> 255 ^ 255 = 0;

   And 128 ^ 255 = 127 ...

Signature

Eric.Sosman@sun.com

Grant Wagner - 11 May 2005 21:05 GMT
>>>If I have a Color object, is it possible to get a constrasting Color
>>>in
[quoted text clipped - 9 lines]
>
>    And 128 ^ 255 = 127 ...

Why 128 ^ 255 = 127 is a bad thing is spelled out in more detail at
<url: http://blogs.msdn.com/oldnewthing/archive/2004/11/25/270320.aspx 
/>.

Signature

Grant Wagner <gwagner@agricoreunited.com>

Fred L. Kleinschmidt - 11 May 2005 15:41 GMT
> If I have a Color object, is it possible to get a constrasting Color in
> return?
>
> Black: 255 255 255 becomes White: 0 0 0 so on and so forth...

In general this is a very complex problem. You not only want a
contrasting color - you also want one that "looks good" against the base
color. It is possible, but it takes several hunbdred lines of code.

A good reference for how to do this is Motif's XmGetColors() function.

Signature

Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94  (206)544-5225



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.