Skip to content

Instantly share code, notes, and snippets.

@hypothermic
Created June 7, 2022 15:26
Show Gist options
  • Save hypothermic/aa826a654a01bc76041dd1efe746d39d to your computer and use it in GitHub Desktop.
Save hypothermic/aa826a654a01bc76041dd1efe746d39d to your computer and use it in GitHub Desktop.
Hue value to RGB color in pure VHDL
-- Loosely based off https://stackoverflow.com/a/34663498
-- but implemented with fixed arithmatic
-- to fit the 12-bit value of a VGA color
signal color : std_logic_vector (12-1 downto 1-1);
color_proc : process (clk, clk_en) is
begin
if rising_edge(clk) then
if clk_en = '1' then
if hue < 15 then
hue <= hue + 1;
else
hue <= 0;
end if;
end if;
end if;
end process;
color <= hue2rgb(hue);
pure function floor(hue : integer) return integer is
begin
if hue < 15 then
return 0;
elsif hue < 30 then
return 15;
elsif hue < 45 then
return 30;
else
return 45;
end if;
end function;
pure function clamp(val : integer; lo : integer; hi : integer) return integer is
begin
if val < lo then
return lo;
elsif val > hi then
return hi;
else
return val;
end if;
end function;
pure function channel(hue : integer; ch : integer) return std_logic_vector is
begin
return std_logic_vector(to_unsigned(
clamp(
6 * abs((hue + (5 * ch)) - floor((hue + (5 * ch))) - 8) - 15,
0,
15
), 4
));
end function;
pure function hue2rgb(hue : integer) return std_logic_vector is
begin
return channel(hue, 0) & channel(hue, 1) & channel(hue, 2);
end function;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment