Color wheel: Difference between revisions
→{{header|Applesoft BASIC}}
Antoni Gual (talk | contribs) (added image) |
|||
(14 intermediate revisions by 8 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}}==
The lo-resolution GRaphics screen is limited to 16 colors. Ordered dithering is used for the saturation. Pink is mixed with violet and magenta, and aqua is mixed with light blue and green. These lighter colors are randomly mixed with the saturation dither. Note that the four Apple II colors can be seen at the 90 degree marks: orange at 30 degrees, green at 120 degrees, cyan (blue) at
<syntaxhighlight lang="gwbasic"> 100 LET R = 3.1415926535 / 180
110 LET YO = 20
Line 39 ⟶ 132:
370 IF SCRN( X,Y) = 0 THEN PLOT X,Y:P = P + 1: IF P > = 9 THEN P = 0
380 NEXT I,S</syntaxhighlight>
=={{header|AppleScript}}==
Line 148 ⟶ 242:
{{out}}
[[Media:Colorwheel cpp.png]]
=={{header|C#}}==
<syntaxhighlight lang="csharp">
// constructor of main window
// in MainWindow.xaml just create <Image Name="imgMain" />
public MainWindow()
{
InitializeComponent();
RenderOptions.SetBitmapScalingMode(imgMain, BitmapScalingMode.HighQuality);
imgMain.Source = new WriteableBitmap(480, 480, 96, 96, PixelFormats.Bgr32, null);
// using slider you can change saturation and call DrawHue with different level
DrawHue(100);
}
void DrawHue(int saturation)
{
var bmp = (WriteableBitmap)imgMain.Source;
int centerX = (int)bmp.Width / 2;
int centerY = (int)bmp.Height / 2;
int radius = Math.Min(centerX, centerY);
int radius2 = radius - 40;
bmp.Lock();
unsafe{
var buf = bmp.BackBuffer;
IntPtr pixLineStart;
for(int y=0; y < bmp.Height; y++){
pixLineStart = buf + bmp.BackBufferStride * y;
double dy = (y - centerY);
for(int x=0; x < bmp.Width; x++){
double dx = (x - centerX);
double dist = Math.Sqrt(dx * dx + dy * dy);
if (radius2 <= dist && dist <= radius) {
double theta = Math.Atan2(dy, dx);
double hue = (theta + Math.PI) / (2.0 * Math.PI);
*((int*)(pixLineStart + x * 4)) = HSB_to_RGB((int)(hue * 360), saturation, 100);
}
}
}
}
bmp.AddDirtyRect(new Int32Rect(0, 0, 480, 480));
bmp.Unlock();
}
static int HSB_to_RGB(int h, int s, int v)
{
var rgb = new int[3];
var baseColor = (h + 60) % 360 / 120;
var shift = (h + 60) % 360 - (120 * baseColor + 60 );
var secondaryColor = (baseColor + (shift >= 0 ? 1 : -1) + 3) % 3;
//Setting Hue
rgb[baseColor] = 255;
rgb[secondaryColor] = (int) ((Math.Abs(shift) / 60.0f) * 255.0f);
//Setting Saturation
for (var i = 0; i < 3; i++)
rgb[i] += (int) ((255 - rgb[i]) * ((100 - s) / 100.0f));
//Setting Value
for (var i = 0; i < 3; i++)
rgb[i] -= (int) (rgb[i] * (100-v) / 100.0f);
return RGB2int(rgb[0], rgb[1], rgb[2]);
}
public static int RGB2int(int r, int g, int b) => r << 16 | g << 8 | b;
</syntaxhighlight>
{{out}}
[[File:ColorRing.PNG]]
=={{header|Delphi}}==
Line 254 ⟶ 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 728 ⟶ 950:
<syntaxhighlight lang="lua">
local function hsv_to_rgb (h, s, v) -- values in ranges: [0, 360], [0, 1], [0, 1]
end
function love.load()
end
end
end
function love.draw()
end
</syntaxhighlight>
Line 1,061 ⟶ 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,119 ⟶ 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,156 ⟶ 1,401:
} ).map: ((*+$m) * 255).Int
}</syntaxhighlight>
=={{header|Ring}}==
Line 1,290 ⟶ 1,533:
' -----------------------------------
global pi
pi
steps
graphic #g, 525, 525
Line 1,297 ⟶ 1,540:
for x =0 to 525 step steps
next x
render #g
Line 1,325 ⟶ 1,568:
function atan2(y,x)
if (x = 0) and (y <> 0) then
end if
if y = 0 and (x <> 0) then
end if
If r$ <> "Y" then
end if
end function</syntaxhighlight>
Line 1,497 ⟶ 1,740:
This file is no longer there!!! 10 Sep 2021
=={{header|Vala}}==
{{trans|Julia}}
<syntaxhighlight lang="vala">public class Example: Gtk.Application {
private Gtk.ApplicationWindow window;
private Gtk.DrawingArea drawing_area;
public Example() {
Object(application_id: "my.application", flags: ApplicationFlags.FLAGS_NONE);
this.activate.connect(() => {
window = new Gtk.ApplicationWindow(this);
drawing_area = new Gtk.DrawingArea();
drawing_area.set_draw_func(draw_circle);
window.set_child(drawing_area);
window.present();
});
}
private void draw_circle(Gtk.DrawingArea area, Cairo.Context cr, int width, int height) {
int centerx = width / 2;
int centery = height / 2;
double anglestep = 1.0 / width;
for (float theta = (float) 0.0; theta < 360; theta += (float) 0.1) {
float r;
float g;
float b;
Gtk.hsv_to_rgb(theta / (float) 360.0, 1, 1, out r, out g, out b);
cr.set_source_rgb(r, g, b);
cr.line_to(centerx, centery);
cr.arc(centerx, centery, ((double) width) / 2.2, GLib.Math.PI * 2 * theta / 360.0, anglestep);
cr.line_to(centerx, centery);
cr.stroke();
}
}
public static int main(string[] argv) {
var app = new Example();
return app.run(argv);
}
}</syntaxhighlight>
=={{header|VBScript}}==
Building a BMP file and opening it with the default viewer. It takes 5 seconds in my 5 years old notebook. Run with Cscript if you don want to be clicking at annoying message boxes.
Line 1,780 ⟶ 2,061:
</syntaxhighlight>
=={{out}}==
[[File:Colorwheel vbs.png]]
Line 1,785 ⟶ 2,067:
=={{header|Wren}}==
{{libheader|DOME}}
<syntaxhighlight lang="
import "dome" for Window
import "random" for Random
Line 1,876 ⟶ 2,158:
v,hue:=(x - zero).toFloat().toPolar(y - zero);
if(v<=R){ // only render in the circle
}
}
Line 1,884 ⟶ 2,166:
fcn hsv2rgb(hue,v,s){ // 0<=H<360, 0<=v(brightness)<=1, 0<=saturation<=1
to24bit:=fcn(r,g,b,m){
r,g,b=((r+m)*255).toInt(),((g+m)*255).toInt(),((b+m)*255).toInt();
Line 1,897 ⟶ 2,179:
else if(180<=hue<240) return(to24bit(0.0,x, c, m));
else if(240<=hue<300) return(to24bit(x, 0.0,c, m));
else
}</syntaxhighlight>
{{out}}
|