Note that there are some explanatory texts on larger screens.

plurals
  1. POConvert double value to RGB Color in c#
    primarykey
    data
    text
    <p>I am trying to convert a double value (which is between 0 and 1) to RGB Color. In the code below you can see what Im trying to do but I feel that there is something wrong with this algorithm. Im not getting all the colors. Maybe there is a loose of information when I convert from double to int or Im not sure...but please have a look at it and if you have any suggestion or any other method(validated one) please let me know:</p> <pre><code> private Color generateRGB(double X) { Color color; if (X &gt;= 0.5) //red and half of green colors { int Red = (int)((2 * X - 1) * 255); int Green = (int)((2 - 2 * X) * 255); int Blue = 0; color = Color.FromArgb(Red, Green, Blue); } else // blue and half of green colors { int Red = 0; int Green = (int)((2 * X) * 255); int Blue = (int)((1 - 2 * X) * 255); color = Color.FromArgb(Red, Green, Blue); } return color; } </code></pre> <p>Here is the image that expresses the best what im trying to do. </p> <p><a href="https://www.dropbox.com/s/bvs3a9m9nc0rk5e/20131121_143044%20%281%29.jpg" rel="nofollow">https://www.dropbox.com/s/bvs3a9m9nc0rk5e/20131121_143044%20%281%29.jpg</a></p> <p>[Updated] </p> <p>That`s how I did and it seems a good solution. Please have a look at it and tell me weather this is better representation or not (maybe those who have a better knowledge for Color Spaces could give a feedback about it)</p> <p>I have used an HSVtoRGB conversion algorithm from here: <a href="http://www.splinter.com.au/converting-hsv-to-rgb-colour-using-c/" rel="nofollow">http://www.splinter.com.au/converting-hsv-to-rgb-colour-using-c/</a>. Knowing that my values are at [0,1] interval, I<code>m extending this interval to [0, 360] in order to use the algorithm for converting HSV to RGB. I</code>m using s and v equal to 1 in my case. Here is the code for better explanation.</p> <pre><code> private Color generateRGB(double X) { Color color; int red; int green; int blue; HsvToRgb(X*360,1,1,out red,out green,out blue); color = Color.FromArgb(red, green, blue); return color; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload