Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This function finds the max value of a range of floats.
- private float Max(params float[] numbers)
- {
- float max = Single.MinValue;
- foreach (float num in numbers)
- {
- if (num > max)
- {
- max = num;
- }
- }
- return max;
- }
- //This function finds the min value of a range of floats.
- private float Min(params float[] numbers)
- {
- float min = Single.MaxValue;
- foreach (float num in numbers)
- {
- if (num < min)
- {
- min = num;
- }
- }
- return min;
- }
- void RgbToHsv(float r, float g, float b, out float h, out float s, out float v)
- {
- float min, max, delta;
- min = Min(r, g, b);
- max = Max(r, g, b);
- v = max;
- delta = max - min;
- if (max != 0)
- {
- s = delta / max;
- }
- else
- {
- s = 0;
- h = 0;
- return;
- }
- if (r == max)
- {
- h = (g - b) / delta; // between yellow & magenta
- }
- else if (g == max)
- {
- h = 2 + (b - r) / delta; // between cyan & yellow
- }
- else
- {
- h = 4 + (r - g) / delta; // between magenta & cyan
- }
- h *= 60; // degrees
- if (h < 0)
- {
- h += 360;
- }
- }
- void HsvToRgb(out float r, out float g, out float b, float h, float s, float v)
- {
- int i;
- float f, p, q, t;
- //if there is no saturation, return a shade of gray.
- if (s == 0)
- {
- r = v;
- g = v;
- b = v;
- return;
- }
- h /= 60; // sector 0 to 5
- i = (int)Math.Floor(h);
- f = h - i; // factorial part of h
- p = v * (1 - s);
- q = v * (1 - s * f);
- t = v * (1 - s * (1 - f));
- switch (i)
- {
- case 0:
- r = v;
- g = t;
- b = p;
- break;
- case 1:
- r = q;
- g = v;
- b = p;
- break;
- case 2:
- r = p;
- g = v;
- b = t;
- break;
- case 3:
- r = p;
- g = q;
- b = v;
- break;
- case 4:
- r = t;
- g = p;
- b = v;
- break;
- default:
- r = v;
- g = p;
- b = q;
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement