Skip to content

Instantly share code, notes, and snippets.

@ytomino
Created March 6, 2011 07:24
Show Gist options
  • Save ytomino/857105 to your computer and use it in GitHub Desktop.
Save ytomino/857105 to your computer and use it in GitHub Desktop.
RGB - HSV conversion
package body Colors is
function To_HSV (Source : RGB) return HSV is
Max : constant Lightness := Lightness'Max (Lightness'Max (Source.Blue, Source.Green), Source.Red);
Min : constant Lightness := Lightness'Min (Lightness'Min (Source.Blue, Source.Green), Source.Red);
Diff : constant Lightness := Max - Min;
Result : HSV;
begin
Result.Value := Max;
if Max > Min then
Result.Saturation := Diff / Max;
declare
Temp_Hue : Hue'Base;
begin
if Source.Blue = Max then
Result.Hue := 4.0 + (Source.Red - Source.Green) / Diff;
elsif Source.Green = Max then
Result.Hue := 2.0 + (Source.Blue - Source.Red) / Diff;
else
Temp_Hue := (Source.Green - Source.Blue) / Diff;
if Temp_Hue < 0.0 then
Temp_Hue := Temp_Hue + 6.0;
end if;
end if;
Result.Hue := Temp_Hue * (2.0 * Ada.Numerics.Pi) / 6.0;
end;
else
Result.Saturation := 0.0;
Result.Hue := 0.0;
end if;
return Result;
end To_HSV;
function To_RGB (Source : HSV) return RGB is
Diff : Lightness;
N1, N2, N3: Lightness;
Result : RGB;
begin
if Source.Saturation = 0.0 then
Result.Red := Source.Value;
Result.Green := Source.Value;
Result.Blue := Source.Value;
else
declare
Temp_Diff : constant Lightness'Base := Source.Hue * 6.0 / (2.0 * Ada.Numerics.Pi);
begin
Diff := Temp_Diff - Lightness'Floor (Temp_Diff);
end;
N1 := Source.Value * (1.0 - Source.Saturation);
N2 := Source.Value * (1.0 - Source.Saturation * Diff);
N3 := Source.Value * (1.0 - Source.Saturation * (1.0 - Diff));
declare
Pi : constant := Ada.Numerics.Pi;
V : Lightness renames Source.Value;
begin
if Source.Hue = 0.0 then Result.Red := V ; Result.Green := N1; Result.Blue := N1;
elsif Source.Hue < Pi / 3.0 then Result.Red := V ; Result.Green := N3; Result.Blue := N1;
elsif Source.Hue = Pi / 3.0 then Result.Red := V ; Result.Green := V ; Result.Blue := N1;
elsif Source.Hue < Pi * 2.0 / 3.0 then Result.Red := N2; Result.Green := V ; Result.Blue := N1;
elsif Source.Hue = Pi * 2.0 / 3.0 then Result.Red := N1; Result.Green := V ; Result.Blue := N1;
elsif Source.Hue < Pi then Result.Red := N1; Result.Green := V ; Result.Blue := N3;
elsif Source.Hue = Pi then Result.Red := N1; Result.Green := V ; Result.Blue := V;
elsif Source.Hue < Pi * 4.0 / 3.0 then Result.Red := N1; Result.Green := N2; Result.Blue := V;
elsif Source.Hue = Pi * 4.0 / 3.0 then Result.Red := N1; Result.Green := N1; Result.Blue := V;
elsif Source.Hue < Pi * 5.0 / 3.0 then Result.Red := N3; Result.Green := N1; Result.Blue := V;
elsif Source.Hue = Pi * 5.0 / 3.0 then Result.Red := V ; Result.Green := N1; Result.Blue := V;
else Result.Red := V ; Result.Green := N1; Result.Blue := N2;
end if;
end;
end if;
return Result;
end To_RGB;
end Colors;
with Ada.Numerics;
package Colors is
pragma Pure;
subtype Lightness is Float range 0.0 .. 1.0;
subtype Hue is Float range 0.0 .. 1.0;
subtype Saturation is Float range 0.0 .. 2.0 * Ada.Numerics.Pi;
type RGB is record
Red : Lightness;
Green : Lightness;
Blue : Lightness;
end record;
type HSV is record
Hue : Colors.Hue;
Saturation : Colors.Saturation;
Value : Lightness;
end record;
function To_HSV (Source : RGB) return HSV;
function To_RGB (Source : HSV) return RGB;
end Colors;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment