Color wheel: Difference between revisions

 
(7 intermediate revisions by 5 users not shown)
Line 7:
 
 
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">
with Ada.Numerics; use Ada.Numerics;
with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions;
with Ada.Text_IO; use Ada.Text_IO;
procedure Color_Wheel is
type Colour_Level is mod 2 ** 8;
type RGB is record
R, G, B : Colour_Level;
end record;
BLACK : constant RGB := (0, 0, 0);
type Image_Grid is array (Integer range <>, Integer range <>) of RGB;
 
Diameter : constant Integer := 480;
Radius : constant Integer := Diameter / 2;
Radius_Fl : constant Float := Float (Radius);
Image : Image_Grid (-Radius .. Radius, -Radius .. Radius);
V : constant Float := 1.0;
 
procedure Write_PPM (Grid : Image_Grid; Filename : String) is
PPM_File : File_Type;
begin
Create (PPM_File, Out_File, Filename);
Put_Line (PPM_File, "P3");
Put_Line (PPM_File, Grid'Length (1)'Image & Grid'Length (2)'Image);
Put_Line (PPM_File, "255");
for Y in reverse -Radius .. Radius loop
for X in -Radius .. Radius loop
Put_Line (PPM_File, Grid (X, Y).R'Image & Grid (X, Y).G'Image & Grid (X, Y).B'Image);
end loop;
end loop;
Close (PPM_File);
end Write_PPM;
 
function Atan2 (Y, X : Float) return Float is
Res : Float;
begin
if X > 0.0 then Res := Arctan (Y / X);
elsif X < 0.0 and then Y >= 0.0 then Res := Arctan (Y / X) + Pi;
elsif X < 0.0 and then Y < 0.0 then Res := Arctan (Y / X) - Pi;
elsif X = 0.0 and then Y > 0.0 then Res := Pi / 2.0;
elsif X = 0.0 and then Y > 0.0 then Res := -Pi / 2.0;
else Res := -Pi / 2.0; -- Technically: Undefined
end if;
return Res;
end Atan2;
begin
for Y in -Radius .. Radius loop
for X in -Radius .. Radius loop
declare
XX : constant Float := Float (X);
YY : constant Float := Float (Y);
Dist : constant Float := Sqrt (XX ** 2 + YY ** 2);
Hue_Int, Hue_Frac, P, Q, T : Float;
Point : RGB;
begin
if Dist <= Radius_Fl then
declare
Sat : constant Float := Dist / Radius_Fl;
Hue : Float := Atan2 (YY, XX);
RR, GG, BB : Float;
begin
if Hue < 0.0 then Hue := Hue + 2.0 * Pi; end if;
Hue := (Hue * 180.0 / Pi) / 60.0;
Hue_Int := Float'Floor (Hue);
Hue_Frac := Hue - Hue_Int;
P := V - Sat;
Q := V - Sat * Hue_Frac;
T := V - Sat * (V - Hue_Frac);
case Integer (Hue_Int) is
when 0 => RR := V; GG := T; BB := P;
when 1 => RR := Q; GG := V; BB := P;
when 2 => RR := P; GG := V; BB := T;
when 3 => RR := P; GG := Q; BB := V;
when 4 => RR := T; GG := P; BB := V;
when 5 => RR := V; GG := P; BB := Q;
when others => null;
end case;
Point.R := Colour_Level (Integer (Float'Floor (RR * 255.0)));
Point.G := Colour_Level (Integer (Float'Floor (GG * 255.0)));
Point.B := Colour_Level (Integer (Float'Floor (BB * 255.0)));
Image (X, Y) := Point;
end;
else
Image (X, Y) := BLACK;
end if;
end;
end loop;
end loop;
Write_PPM (Image, "color_wheel.ppm");
end Color_Wheel;
</syntaxhighlight>
 
=={{header|Applesoft BASIC}}==
Line 329 ⟶ 422:
{{out}}
Png Image [https://ibb.co/T0w8KyF].
=={{header|EasyLang}}==
[https://easylang.dev/show/#cod=bZLdboMwDIXveYpzSagaCrTVqi4Pw08CSHSBkG3w9pOBCjK4AKJzPtvBdmt0jqrPYlNmqL4l+tQiMzU4DEpk4B6ACgI+uWeoRmtDJEOAO5kKAtXqkNRCTEkC+BHOlJOR3O1kBFCTZY+s6agYe19hU6FWk3KhMwCzhD9RQsDiiQwCLbmyWdhow3YLOcfs2XjDtjvWOmxywHYLmZnaYa8b1i5s+5/t5eFfvbmOXO5xr6XZ5b+VlA34PCilDUbqC6zG9fFYEhWkjTgjvr07RuSwJwkmeXBgUuueZtR3xsIvBgTEnShzgGJkK1irmf0UbgIaciVtCoHUpl8xhRaD49OGCfgzdkL0cWEIkdzdLNtt9adSIVViiOaVdeBcN9okB8ZL/0gMCHHDSG/HNDK3uPCEntXg3uZLA5h773l/ Run it]
 
{{trans|Go}}
<syntaxhighlight>
proc hsb2rgb hue sat bri . r g b .
h = (hue - floor hue) * 6
f = h - floor h
p = bri * (1 - sat)
q = bri * (1 - sat * f)
t = bri * (1 - sat * (1 - f))
h = floor h
if h = 0
r = bri ; g = t ; b = p
elif h = 1
r = q ; g = bri ; b = p
elif h = 2
r = p ; g = bri ; b = t
elif h = 3
r = p ; g = q ; b = bri
elif h = 4
r = t ; g = p ; b = bri
else
r = bri ; g = p ; b = q
.
.
proc cwheel . .
for y = 0 to 499
dy = y - 250
for x = 0 to 499
dx = x - 250
dist = sqrt (dx * dx + dy * dy)
if dist <= 250
theta = atan2 dy dx
hue = (theta + 180) / 360
hsb2rgb hue (dist / 250) 1 r g b
color3 r g b
move x / 5 y / 5
rect 0.3 0.3
.
.
.
.
cwheel
 
</syntaxhighlight>
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Color_wheel}}
 
'''Solution'''
 
[[File:Fōrmulæ - Color wheel 01.png]]
 
'''Test case'''
 
Generating a color wheel of 300x300 pixels:
 
[[File:Fōrmulæ - Color wheel 02.png]]
 
[[File:Fōrmulæ - Color wheel 03.png]]
 
=={{header|FreeBASIC}}==
Line 1,131 ⟶ 1,283:
c = color(int(h * 255), int(s * 255), 255)
set(x, y, c) # note set() used as Processing set() not as Python set()</syntaxhighlight>
 
 
=={{header|Plain English}}==
<syntaxhighlight lang="plainenglish">
To draw the color wheel:
Start with the red color.
Turn right 80 points.
Loop.
If the user clicks on the screen, break.
Move to the center of the screen.
Draw a line 2 inches long.
Refresh the screen.
Change the current hue by 10 points.
Turn right 10 points.
Add 1 to a count.
If the count is 384, break. \ Plain English uses a circle divided into 384 degrees
Repeat.
Start in the middle of the screen facing north minus 80 points.
Use medium-sized letters.
Write "RED......YELLOW.....GREEN......CYAN......BLUE.....MAGENTA......" with the white pen 2-1/4 inches from the screen's center.
Refresh the screen.
Shut down.
</syntaxhighlight>
 
=={{header|Python}}==
Line 1,189 ⟶ 1,364:
(formerly Perl 6)
{{works with|Rakudo|2016.08}}
[[File:Color-wheel-perl6.png|thumb]]
 
<syntaxhighlight lang="raku" line>use Image::PNG::Portable;
 
Line 1,226 ⟶ 1,401:
} ).map: ((*+$m) * 255).Int
}</syntaxhighlight>
 
Until local image uploading is re-enabled, see [https://github.com/thundergnat/rc/blob/master/img/Color-wheel-perl6.png Color-wheel-perl6.png]
 
=={{header|Ring}}==
Line 1,894 ⟶ 2,067:
=={{header|Wren}}==
{{libheader|DOME}}
<syntaxhighlight lang="ecmascriptwren">import "graphics" for Canvas, Color
import "dome" for Window
import "random" for Random
19

edits