This topic is 3726 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 No artifact going in the other direction, though: 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.

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
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.

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5

• 12
• 9
• 12
• 37
• 12
• ### Forum Statistics

• Total Topics
631415
• Total Posts
2999964
×