Color wheel: Difference between revisions
Content deleted Content added
Antoni Gual (talk | contribs) added image |
Added C# implementation |
||
Line 148:
{{out}}
[[Media:Colorwheel cpp.png]]
=={{header|C#}}==
<syntaxhighlight lang="csharp">
// constructor of main window
public MainWindow()
{
InitializeComponent();
RenderOptions.SetBitmapScalingMode(imgMain, BitmapScalingMode.HighQuality);
imgMain.Source = new WriteableBitmap(480, 480, 96, 96, PixelFormats.Bgr32, null);
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 728 ⟶ 801:
<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,290 ⟶ 1,363:
' -----------------------------------
global pi
pi
steps
graphic #g, 525, 525
Line 1,297 ⟶ 1,370:
for x =0 to 525 step steps
next x
render #g
Line 1,325 ⟶ 1,398:
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,876 ⟶ 1,949:
v,hue:=(x - zero).toFloat().toPolar(y - zero);
if(v<=R){ // only render in the circle
}
}
Line 1,884 ⟶ 1,957:
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 ⟶ 1,970:
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}}
|