Jump to content
  • Advertisement
Sign in to follow this  
myvraccount

C# Color Wheel

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Does anyone know:

 

- What kind of object would I use in C# to make a little box of any color appear, such that if the user clicks it, a window displays with a color wheel and a palette of colors that can be clicked to choose one to update the color of the original box?

 

- How can I implement a restriction so that the color must be chosen within a narrow range (such as a hue between 90 and 120 degrees or a saturation of at least 75%, etc)?

Edited by myvraccount

Share this post


Link to post
Share on other sites
Advertisement

You'll need to provide some more detail here.  What is the program you're trying to create and what technology are you using?  Currently your question just doesn't make sense - C# is a language and does not contain any object which might act as a colour picker.

 

There are colour picker controls that have been developed for different technologies and you can look for solutions on creating something on a given platform but you'll need to specify what you're working with.

Share this post


Link to post
Share on other sites

Well I didn't mean to imply that it was necessarily built-in to the C# libraries.  A plugin is fine if I need to go that route, but I'd like something as simple an generic as possible, but with the ability to limit the range in color space (ideally in HSL if possible).

Share this post


Link to post
Share on other sites

I'm sorry, but I'm really not comfortable divulging the purpose of it.  I understand that in some cases it could make it easier to understand the best way to do it, but in this case I've told you all that is necessary.  I really want nothing more than a color picker that can restrict the range of colors that a user can pick.  What more clarification do I need?  The reason is not important, but if anyone has a way to accomplish this, I'm all ears.

Share this post


Link to post
Share on other sites

I'm sorry, but I'm really not comfortable divulging the purpose of it. I understand that in some cases it could make it easier to understand the best way to do it, but in this case I've told you all that is necessary. I really want nothing more than a color picker that can restrict the range of colors that a user can pick. What more clarification do I need? The reason is not important, but if anyone has a way to accomplish this, I'm all ears.

The problem here is that there are a few thousand different methods to do this depending on what you are using to interact with the user. For example the most common approach would be to use winforms but you can also use mono, unity or pretty much anything else ever concieved to interact with the user. For all we know you might be using a COM interface to make a grid of leds go on and off. Or if you don't have anything at all then I suggest you start looking at winforms tutorials and when you have a window with some interactivity you come back and tell us what you used to make the window and we will happily figure out a way to make a color picker for that. Making a simple color picker from nothing at all is several months of work even for a professional. Even if we picked a library at random and made a ready to use color picker for it it would propably take a long long time for you to integrate it into your project. So help us help you and give us little more details.

You are right that you don't have to tell us what your project is about but everyone else is also rigth that we need more information to be able to help.

Share this post


Link to post
Share on other sites

A good starting point would be to just draw a color wheel to a window.

 

If you're using Windows Forms, you can use a Bitmap object to draw the graphic. You can get access to the image pixels and fill them yourself.

 

Since Bitmap is usually represented as RGB(a) values, you would probably find RGB<->HSL conversion functions useful. A simple HSL to RGB function divides the circle into six sectors and performs linear interpolation over the nearest sector to the hue angle as triangle. Saturation is simply linear interpolation between gray and the found hue, and lightness is interpolation between black, the color and white.

 

The "wheel" is a function of the angle and the distance from the center point. You can use atan (or atan2) to find an angle of a pixel relative to the center of the bitmap. This angle would be the hue angle parameter (as described in the previous paragraph). Lightness or saturation, could be the Euclidean distance of the pixel from the center. Either one could also be represented as a separate slider, because you cannot represent all three dimensions (h, s and l) in the same 2d area.

 

When you manage to draw the wheel, getting the RGB value given a clicked HSL point becomes simply evaluating the HSL to RGB conversion function at the clicked point. The restrictions that you mention can be implemented by simply restricting the selected parameters to given ranges.

 

The actual coding takes about half a day to a day, if you understand the concepts. Googling will readily find RGB-HSL conversion functions as well as the atan and distance functions, if they are not familiar. The .net reference documentation includes the Bitmap class documentation, which explains how to fill a bitmap manually in memory (as opposed to loading it from a file).

Edited by Nik02

Share this post


Link to post
Share on other sites

Oh well if it doesn't matter what it's used for, how about using the standard color picker dialog defaulted to display the color grid using the custom colors property  (ok, it's not a wheel, but it's close). After the user selects a color then check the hue/saturation/etc. If it's outside the acceptable range re-open the dialog. Maybe pop-up a message dialog saying the value chosen is invalid and to try again. Technically fits most of your requirements.

Share this post


Link to post
Share on other sites

Please find attached a sample which plots hue and saturation over x and y. Took half an hour to make it.

 

All and any improvement is left as an exercise to the reader smile.png

 

The code included herein is a C# project with .net 4.5.1 Windows Forms target. Should open in Visual Studio 2013 or later.

 

I was lazy with the HSL->RGB conversion and ported it from a StackOverflow article because it was more elegant than my old code. Link is in the source.

Edited by Nik02

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!