Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a built-in C#/.NET System API for HSV to RGB?

Tags:

c#

.net

colors

Is there an API built into the .NET framework for converting HSV to RGB? I didn't see a method in System.Drawing.Color for this, but it seems surprising that there wouldn't be one in the platform.

like image 787
jsight Avatar asked Aug 26 '09 15:08

jsight


People also ask

What is built-in in C?

Variable of the built-in types are binary compatible with the corresponding C types. These classes define appropriate creation routines which may be used for convenient casting between Sather and C types. Also, many basic operations on the built-in C types are provided by the library.

Is everything built on C?

Having said all that, the answer to your question is "No". C is based off of a language called ALGOL, and there were many competitors both with ALGOL (FORTRAN, Lisp, COBOL) and C (none come to mind).

What is built-in data type in C?

ANSI C provides three types of data types: Primary(Built-in) Data Types: void, int, char, double and float. Derived Data Types: Array, References, and Pointers.

Is C made in assembly?

The origin of C is closely tied to the development of the Unix operating system, originally implemented in assembly language on a PDP-7 by Dennis Ritchie and Ken Thompson, incorporating several ideas from colleagues. Eventually, they decided to port the operating system to a PDP-11.


1 Answers

There isn't a built-in method for doing this, but the calculations aren't terribly complex.
Also note that Color's GetHue(), GetSaturation() and GetBrightness() return HSL values, not HSV.

The following C# code converts between RGB and HSV using the algorithms described on Wikipedia.
I already posted this answer here, but I'll copy the code here for quick reference.

The ranges are 0 - 360 for hue, and 0 - 1 for saturation or value.

public static void ColorToHSV(Color color, out double hue, out double saturation, out double value) {     int max = Math.Max(color.R, Math.Max(color.G, color.B));     int min = Math.Min(color.R, Math.Min(color.G, color.B));      hue = color.GetHue();     saturation = (max == 0) ? 0 : 1d - (1d * min / max);     value = max / 255d; }  public static Color ColorFromHSV(double hue, double saturation, double value) {     int hi = Convert.ToInt32(Math.Floor(hue / 60)) % 6;     double f = hue / 60 - Math.Floor(hue / 60);      value = value * 255;     int v = Convert.ToInt32(value);     int p = Convert.ToInt32(value * (1 - saturation));     int q = Convert.ToInt32(value * (1 - f * saturation));     int t = Convert.ToInt32(value * (1 - (1 - f) * saturation));      if (hi == 0)         return Color.FromArgb(255, v, t, p);     else if (hi == 1)         return Color.FromArgb(255, q, v, p);     else if (hi == 2)         return Color.FromArgb(255, p, v, t);     else if (hi == 3)         return Color.FromArgb(255, p, q, v);     else if (hi == 4)         return Color.FromArgb(255, t, p, v);     else         return Color.FromArgb(255, v, p, q); } 
like image 88
Greg Avatar answered Sep 20 '22 11:09

Greg