aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes HSV gradient Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "HSV gradient" Watch "HSV gradient" New topic
Author

HSV gradient

Steven Sørensen
Greenhorn

Joined: Apr 01, 2009
Posts: 3
Hi there.
I'm having a bit of trouble finding a way to generate an HSV gradient...
sRGB and Linear RGB gradients are simple enough, as they're predefined in the java.awt.MultipleGradientPaint class and whatnot. But I can't for the life of me find a way to use the HSV (or HSB or HSL) colorspace to blend two colors. (And frustratingly, java.awt.color.ColorSpace isn't implemented out-of-the-box for common colorspaces. Arg.)

RGB gradient? Easy enough, it's implied:

But the HSV colorspace? There certainly doesn't seem to be any obvious way to to use it. Am I missing something right in front of me?

The best I could dig up with Google was this page: http://users.erols.com/ziring/java-samp-jgd.html It's pretty good, and the source code is very clean, but it's doesn't use the java.awt.Paint interface. I'm not sure how to retrofit it with Graphics2D compatibility.
Everything else I saw on Google was trivial algorithms to convert one colorspace to another. Not helpful.

Any pointers?



Edit: Okay, now this is extra sad. This post comes up in spot #1 in a Google search for java gradient hsb OR hsv OR hsl ...
Steven Sørensen
Greenhorn

Joined: Apr 01, 2009
Posts: 3
URGENT!!! plz sir send teh codez god bless

Or "bump" or something.

I'm guessing this means there isn't a nice built-in way to create these gradients...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

http://faq.javaranch.com/java/PatienceIsAVirtue
(I'll ignore the mistyped words since that was obviously meant as a joke )


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
It was 1½ weeks before he asked for a bump, Rob.

You can convert HSB to RGB with methods in the Color class; you might be able to use those to pass H, S and B values to Color objects and use those objects. Another Color link here.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Campbell Ritchie wrote:It was 1½ weeks before he asked for a bump, Rob.

You're right, it should have been Ease Up. My bad.
Steven Sørensen
Greenhorn

Joined: Apr 01, 2009
Posts: 3
Yeah, I know bumping isn't considered good form, but I've lurked* here for a while and seen it accepted, so I thought I'd give it a shot.
(*Unintentionally: Google often correctly suggests this place for the solutions to a quite a number of problems I've run into. So kudos for that.)

Campbell Ritchie wrote:You can convert HSB to RGB with methods in the Color class; you might be able to use those to pass H, S and B values to Color objects and use those objects. Another Color link here.


I have tried that, but actually it's just for converting values into the Color class's RGB storage form. (I checked the source code to be sure!) The GradientPaint class is what does the blending dirty work.
Just yesterday I started playing around with the Processing framework/library/API/whathaveyou. It's pretty great and has a really easy-to-use (read: made-for-n00bs) color handling that can easily blend on in HSB space.
Although I'm a bit disappointed that there seems to be no out-of-the-box Java way of doing this, I'll be using the Processing methods from now on, which are entirely sufficient.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Steven Sørensen wrote:Yeah, I know bumping isn't considered good form, but I've lurked* here for a while and seen it accepted, so I thought I'd give it a shot.

Bumping is allowed after 24 hours, but the "URGENT" was what I wanted to address. I just posted the wrong link.
Scott Phan
Greenhorn

Joined: Jul 23, 2009
Posts: 1
Here's an example of an HSV gradient track I wrote using the relationships between RGB and HSV found here: Wiki (HSV and HSL)

eric aro
Greenhorn

Joined: Dec 04, 2009
Posts: 15
here is my best solution :

Override GradientPaint (easy part) to HSVGradientPaint
copy paste/ GradientPaintContext into HSVGradientPaint (easy part)

there is an ugly code from sun that I can't get rid of (if someone has a deep knowledge on how to work on raster ...)

and replace the interp array filling (in the construcor) with one that matches HSV color.

here is the master piece, use this function to fill the "interp" array instead of the original code.


and BTW I would realllllllly like to find a better solution.

eric

(ps: attachement does not work, here is a copy paste of the whole solution )
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: HSV gradient