Jump to content

  • Log In with Google      Sign In   
  • Create Account


[.net] C# PathGradientBrush


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 Numsgil   Members   -  Reputation: 501

Like
0Likes
Like

Posted 02 April 2008 - 11:05 AM

I'm working on a color selector in C# for a particle emitter for a game. Users select a start min and max color, and an end min and max color, and I draw a quad representing those four colors, and all the possible values in between. At present I'm using a PathGradientBrush, and set four points up as the path, and assign colors to each of them. The problem I'm having is a visual artifact: a line that runs along the diagonal of the resultant image. Visual artifact Free Image Hosting at www.ImageShack.us No artifact going in the other direction, though: Free Image Hosting at www.ImageShack.us I know why the artifact appears. It has to do with where the "center" is. Right now I'm setting the center point to the upper left corner, as it minimizes the artifact (only shows up in one direction). I'm not sure how to remove it entirely though. Ideally I'd like something that looks like a quad in DirectX or OpenGL. (clicky), but I can't figure out how to get there. I've tried looking on sites that do C# graphics tutorials, but they all seem to have that same artifact. Here's the code I'm using to set up the PathGradientBrush:
brush = new PathGradientBrush(new PointF[]{
    new PointF(0,                        0),
    new PointF(0,                        ClientRectangle.Height),        //StartB
    new PointF(ClientRectangle.Width,    ClientRectangle.Height),        //EndB
    new PointF(ClientRectangle.Width,    0)});                           //EndA           

brush.CenterPoint = new PointF(0.0f, 0.0f);
brush.CenterColor = StartA.Color;
brush.SurroundColors = new Color[] { StartA.Color, StartB.Color, EndB.Color, EndA.Color };


Any ideas people have would be appreciated.
Darwinbots - Artificial life simulation

Sponsor:

#2 Shannon Barber   Moderators   -  Reputation: 1362

Like
0Likes
Like

Posted 05 April 2008 - 05:30 PM

You need a better anti-aliasing filter... does the PathGradientBrush have any quality properties? You need to enable bilinear filtering, it looks like it's using nearest-neighbor.

Trying setting Graphics.InterpolationMode to high quality.

#3 Numsgil   Members   -  Reputation: 501

Like
0Likes
Like

Posted 05 April 2008 - 10:14 PM

I tried various settings of interpolation mode but none of them have any noticable effect. I don't think this is an aliasing issue. I've seen it in pretty much every tutorial or article with pictures that talks about the pathgradientbrush. More and more I'm thinking it's a logical flaw in the way the brush interpolates between the center and it's edges.

At the moment I'm thinking of doing a four pass drawing using four linear gradient brushes. One would interpolate for each edge, and the results would be alpha blended. So if I had a square that looked like this:


R-G
| |
B-W


with R = red, G = green, B = blue, W = white, you'd do a base layer that was a Red to Blue, then a layer of Green to white, with alpha fading to 0 as it moved left. Then do the same along the vertical direction. Hope I've explained it well.
Darwinbots - Artificial life simulation

#4 Headkaze   Members   -  Reputation: 583

Like
0Likes
Like

Posted 06 April 2008 - 04:47 AM

Any reason why you can't use the ColorDialog class?

Another option is to use a pre-made image (in Photoshop or whatever) with the gradiants and then use a "GetPixel()" method to read the pixel from the bitmap to select the color.

You could have an image like the one you linked to for selecting the hue then you could have two rectangles with Black->Color and White->Color.

#5 Numsgil   Members   -  Reputation: 501

Like
0Likes
Like

Posted 06 April 2008 - 08:45 AM

Two reasons for not using the color dialog:

1. My custom control needs to show four user selected colors being Lerped together. The color dialog wasn't built for that.

2. My team got fed up with the default color dialog, so we built our own :P

Likewise a prebuilt bitmap wouldn't work because the four colors need to be selected at run time. I have, however, also tried outputting to a small bitmap and scaling the bitmap, but while the result is smooth it's a far cry from a true Lerp of the four colors. SetPixel is possible too, but I'm trying to avoid something so brute force.
Darwinbots - Artificial life simulation

#6 Headkaze   Members   -  Reputation: 583

Like
0Likes
Like

Posted 07 April 2008 - 10:40 AM

I have some code that I wrote that can do hue rotation if your interested. Basically you could have a pre-made image with all the shades of a color. Then using my ColorProcessing class you can rotate the hue using a slider. It also supports saturation and brightness. I've used this class in a few of my projects and it's fast enough to work on an image in real time using sliders. So probably ideal for you if you can't get the gradient thing sorted.

ColorProcessing.zip

#7 Numsgil   Members   -  Reputation: 501

Like
0Likes
Like

Posted 07 April 2008 - 12:47 PM

Thanks, but I did actually write my own little control to select a single color graphically using hue, sat, and lum. The problem here is that I have potentially four unique colors in the four corners, meaning that I need some way to bilinearly interpolate between those four colors quickly. And these four colors need to be determinable at run time from the user.

Basically, the user selects the upper left corner, and a dialog pops up, asking for a color choice (where either the default C# colordialog could be used, or a custom color control). Once the user selects a color, the control updates itself and that corner becomes the new color.

I got a hacky solution up right now, where I just brute force the bilinear calculation and SetPixel for each pixel. It takes about a second to draw, so it's something I could live with, but a more manageable draw time would be nice, too.
Darwinbots - Artificial life simulation

#8 Headkaze   Members   -  Reputation: 583

Like
0Likes
Like

Posted 08 April 2008 - 03:21 AM

A second for rendering? Hmm that's a performance drag. I suppose your using Bitmap.LockBits for that. Perhaps a C++ dll would improve that or you could use pointers with unsafe code.

Anyway found this on the Net, not sure if it will be any help

http://msdn2.microsoft.com/en-us/magazine/cc164113.aspx


#9 Niksan2   Members   -  Reputation: 265

Like
0Likes
Like

Posted 10 April 2008 - 04:51 AM

Not the most optimal way, but when I did my photoshop style color picker I ended up having a loop for the height of the image to be displayed and used a LinearGradientBrush/FillRectangle, I might be wrong but it could be that gradient/paths tend to use a fanning/triangle system, hence the results you get, I had to do it my way because of how the HSL works and can't work just using 4 color corners, otherwise the color you pick isn't usually the color you get.

#10 Headkaze   Members   -  Reputation: 583

Like
0Likes
Like

Posted 10 April 2008 - 01:26 PM

Actuallly Niksan2 reminded me of another optimization when using Bitmap.LockBits. For some reason if your looping through a bitmap's bits using Bitmap.Width and Bitmap.Height there is a substancial performance hit. It sounds crazy I know but creating variables for Width and Height and using them in your loops instead will actually improve the speed possibly with noticable effect.

#11 Numsgil   Members   -  Reputation: 501

Like
0Likes
Like

Posted 10 April 2008 - 01:45 PM

Hmm, thanks for the tip. I'll try it out.
Darwinbots - Artificial life simulation




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS