?
Solved

HSB to RGB conversion

Posted on 2009-02-12
5
Medium Priority
?
2,058 Views
Last Modified: 2012-08-13
I want a code to convert HSB to RGB color system in vb net.
H=hue
S=saturation
B=brightness

I've tried various codes which were online (in various languages)
None of them work! so please only post a solution that works.
Thanks
0
Comment
Question by:kouroshparsa
  • 3
  • 2
5 Comments
 

Expert Comment

by:ERobishaw
ID: 23631118
Try: http://www.vbaccelerator.com/home/NET/Code/Libraries/Graphics/HLS_to_RGB/article.asp

(HLS = HSB)
Hue = Hue
L [uminence] = B[rightness]
S [aturation] = Saturation

0
 

Accepted Solution

by:
ERobishaw earned 800 total points
ID: 23636209
Here's a C# Version of the above.

public class HLSRGB
	{
		private byte red = 0;
		private byte green = 0;
		private byte blue = 0;
 
		private float hue = 0;
		private float luminance = 0;
		private float saturation = 0;
 
		public byte Red
		{
			get
			{
				return red;
			}
			set
			{
				red = value;
				ToHLS();
			}		
		}
		public byte Green
		{
			get
			{
				return green;
			}
			set
			{
				green = value;
				ToHLS();
			}
		}
		public byte Blue
		{
			get
			{
				return blue;
			}
			set
			{
				blue = value;
				ToHLS();
			}
		}
		public float Luminance
		{
			get
			{
				return luminance;
			}
			set
			{
				if ((luminance < 0.0f) || (luminance > 1.0f))
				{
					throw new ArgumentOutOfRangeException("Luminance", "Luminance must be between 0.0 and 1.0");
				}
				luminance = value;
				ToRGB();
			}
		}
		public float Hue
		{
			get
			{
				return hue;
			}
			set
			{
				if ((hue < 0.0f) || (hue > 360.0f))
				{
					throw new ArgumentOutOfRangeException("Hue", "Hue must be between 0.0 and 360.0");
				}
				hue = value;
				ToRGB();
			}
		}
		public float Saturation
		{
			get
			{
				return saturation;
			}
			set
			{
				if ((saturation < 0.0f) || (saturation > 1.0f))
				{
					throw new ArgumentOutOfRangeException("Saturation", "Saturation must be between 0.0 and 1.0");
				}
				saturation = value;
				ToRGB();
			}
		}
 
		public Color Color
		{
			get
			{
				Color c = Color.FromArgb(red, green, blue);
				return c;
			}
			set
			{
				red = value.R;
				green = value.G;
				blue = value.B;
				ToHLS();
			}
		}
 
		public void LightenColor(float lightenBy)
		{
			luminance *= (1.0f + lightenBy);
			if (luminance > 1.0f)
			{
				luminance = 1.0f;
			}
			ToRGB();
		}
 
		public void DarkenColor(float darkenBy)
		{
			luminance *= darkenBy;
			ToRGB();
		}
		
 
		public HLSRGB(Color c)
		{
			red = c.R;
			green = c.G;
			blue = c.B;
			ToHLS();
		}
 
		public HLSRGB(float hue, float luminance, float saturation)
		{
			this.hue = hue;
			this.luminance = luminance;
			this.saturation = saturation;
			ToRGB();
		}
 
		public HLSRGB(byte red, byte green, byte blue)
		{
			this.red = red;
			this.green = green;
			this.blue = blue;
		}
 
		public HLSRGB(HLSRGB hlsrgb)
		{
			this.red = hlsrgb.Red;
			this.blue = hlsrgb.Blue;
			this.green = hlsrgb.Green;
			this.luminance = hlsrgb.Luminance;
			this.hue = hlsrgb.Hue;
			this.saturation = hlsrgb.Saturation;
		}
 
		public HLSRGB()
		{
		}
 
		private void ToHLS() 
		{			
			byte minval = Math.Min(red, Math.Min(green, blue));
			byte maxval = Math.Max(red, Math.Max(green, blue));
 
			float mdiff  = (float)(maxval - minval);
			float msum   = (float)(maxval + minval);
   
			luminance = msum / 510.0f;
 
			if (maxval == minval) 
			{
				saturation = 0.0f;
				hue = 0.0f; 
			}   
			else 
			{ 
				float rnorm = (maxval - red) / mdiff;      
				float gnorm = (maxval - green) / mdiff;
				float bnorm = (maxval - blue) / mdiff;   
 
				saturation = (luminance <= 0.5f) ? (mdiff / msum) : (mdiff / (510.0f - msum));
 
				if (red == maxval) 
				{
					hue = 60.0f * (6.0f + bnorm - gnorm);
				}
				if (green == maxval) 
				{
					hue = 60.0f * (2.0f + rnorm - bnorm);
				}
				if (blue  == maxval) 
				{
					hue = 60.0f * (4.0f + gnorm - rnorm);
				}
				if (hue > 360.0f) 
				{
					hue = hue - 360.0f;
				}
			}
		}
 
		private void ToRGB() 
		{
			if (saturation == 0.0) // Grauton, einfacher Fall
			{
				red = (byte)(luminance * 255.0F);
				green = red;
				blue = red;
			}
			else
			{
				float rm1;
				float rm2;
         
				if (luminance <= 0.5f) 
				{
					rm2 = luminance + luminance * saturation;
				}
				else
				{
					rm2 = luminance + saturation - luminance * saturation;
				}
				rm1 = 2.0f * luminance - rm2;
				red   = ToRGB1(rm1, rm2, hue + 120.0f);   
				green = ToRGB1(rm1, rm2, hue);
				blue  = ToRGB1(rm1, rm2, hue - 120.0f);
			}
		}
 
		private byte ToRGB1(float rm1, float rm2, float rh)
		{
			if (rh > 360.0f) 
			{
				rh -= 360.0f;
			}
			else if (rh <   0.0f) 
			{
				rh += 360.0f;
			}
 
			if (rh <  60.0f) 
			{
				rm1 = rm1 + (rm2 - rm1) * rh / 60.0f;   
			}
			else if (rh < 180.0f) 
			{
				rm1 = rm2;
			}
			else if (rh < 240.0f) 
			{
				rm1 = rm1 + (rm2 - rm1) * (240.0f - rh) / 60.0f; 
			}
                   
			return (byte)(rm1 * 255);
		}
		
	}

Open in new window

0
 
LVL 2

Author Comment

by:kouroshparsa
ID: 23637198
Thanks for the reply.
I get errors. I fixed some of them but there are still errors saying that the methods need to return a value:
for example, see the blocks of code in the attached code.
How to fix it?

If I can get this to work, I can translate it to vb net.

        public HSB_RGB(Color c)
        {
            red = c.R;
            green = c.G;
            blue = c.B;
            ToHLS();
        }
 
        public HSB_RGB(float hue, float luminance, float saturation)
        {
            this.hue = hue;
            this.luminance = luminance;
            this.saturation = saturation;
            ToRGB();
        }
 
        public HSB_RGB(byte red, byte green, byte blue)
        {
            this.red = red;
            this.green = green;
            this.blue = blue;
        }

Open in new window

0
 

Expert Comment

by:ERobishaw
ID: 23637274
These are constructors in C#, the equivalent in VB to the "New" Keyword. So you want something like:


Public Sub New (Color c)
 red = c.R
 ...
 
End Sub
 
Then to use this you go:
 
Dim MyHSB_RGB As New HSB_RGB(Color.Brown)
lumins = MyHSB_RGB.Luminescence 

Open in new window

0
 
LVL 2

Author Comment

by:kouroshparsa
ID: 23637839
Yes, it compiles. Thanks ERobishaw.
I noticed that my values are not HSB values; they're HLS values.
Still, for my application it's very good.
though does anyone also know the HSB which i initially was looking for?
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…
Watch the video to learn how one can deal with PST file corruption issue with an outstanding Kernel for Outlook PST Repair Tool easily. Using this tool, non-technical users can swiftly perform the repair process to restore their essential data witho…
Suggested Courses
Course of the Month5 days, 1 hour left to enroll

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question