1
1
using System ;
2
- using System . Drawing ;
2
+ using SkiaSharp ;
3
3
4
4
namespace Algorithms . Other ;
5
5
@@ -22,6 +22,8 @@ namespace Algorithms.Other;
22
22
/// </summary>
23
23
public static class Mandelbrot
24
24
{
25
+ private const byte Alpha = 255 ;
26
+
25
27
/// <summary>
26
28
/// Method to generate the bitmap of the Mandelbrot set. Two types of coordinates
27
29
/// are used: bitmap-coordinates that refer to the pixels and figure-coordinates
@@ -39,7 +41,7 @@ public static class Mandelbrot
39
41
/// <param name="maxStep">Maximum number of steps to check for divergent behavior.</param>
40
42
/// <param name="useDistanceColorCoding">Render in color or black and white.</param>
41
43
/// <returns>The bitmap of the rendered Mandelbrot set.</returns>
42
- public static Bitmap GetBitmap (
44
+ public static SKBitmap GetBitmap (
43
45
int bitmapWidth = 800 ,
44
46
int bitmapHeight = 600 ,
45
47
double figureCenterX = - 0.6 ,
@@ -69,7 +71,7 @@ public static Bitmap GetBitmap(
69
71
$ "{ nameof ( maxStep ) } should be greater than zero") ;
70
72
}
71
73
72
- var bitmap = new Bitmap ( bitmapWidth , bitmapHeight ) ;
74
+ var bitmap = new SKBitmap ( bitmapWidth , bitmapHeight ) ;
73
75
var figureHeight = figureWidth / bitmapWidth * bitmapHeight ;
74
76
75
77
// loop through the bitmap-coordinates
@@ -100,22 +102,22 @@ public static Bitmap GetBitmap(
100
102
/// </summary>
101
103
/// <param name="distance">Distance until divergence threshold.</param>
102
104
/// <returns>The color corresponding to the distance.</returns>
103
- private static Color BlackAndWhiteColorMap ( double distance ) =>
105
+ private static SKColor BlackAndWhiteColorMap ( double distance ) =>
104
106
distance >= 1
105
- ? Color . FromArgb ( 255 , 0 , 0 , 0 )
106
- : Color . FromArgb ( 255 , 255 , 255 , 255 ) ;
107
+ ? new SKColor ( 0 , 0 , 0 , Alpha )
108
+ : new SKColor ( 255 , 255 , 255 , Alpha ) ;
107
109
108
110
/// <summary>
109
111
/// Color-coding taking the relative distance into account. The Mandelbrot set
110
112
/// is black.
111
113
/// </summary>
112
114
/// <param name="distance">Distance until divergence threshold.</param>
113
115
/// <returns>The color corresponding to the distance.</returns>
114
- private static Color ColorCodedColorMap ( double distance )
116
+ private static SKColor ColorCodedColorMap ( double distance )
115
117
{
116
118
if ( distance >= 1 )
117
119
{
118
- return Color . FromArgb ( 255 , 0 , 0 , 0 ) ;
120
+ return new SKColor ( 0 , 0 , 0 , Alpha ) ;
119
121
}
120
122
121
123
// simplified transformation of HSV to RGB
@@ -126,19 +128,19 @@ private static Color ColorCodedColorMap(double distance)
126
128
var hi = ( int ) Math . Floor ( hue / 60 ) % 6 ;
127
129
var f = hue / 60 - Math . Floor ( hue / 60 ) ;
128
130
129
- var v = ( int ) val ;
130
- var p = 0 ;
131
- var q = ( int ) ( val * ( 1 - f * saturation ) ) ;
132
- var t = ( int ) ( val * ( 1 - ( 1 - f ) * saturation ) ) ;
131
+ var v = ( byte ) val ;
132
+ const byte p = 0 ;
133
+ var q = ( byte ) ( val * ( 1 - f * saturation ) ) ;
134
+ var t = ( byte ) ( val * ( 1 - ( 1 - f ) * saturation ) ) ;
133
135
134
136
switch ( hi )
135
137
{
136
- case 0 : return Color . FromArgb ( 255 , v , t , p ) ;
137
- case 1 : return Color . FromArgb ( 255 , q , v , p ) ;
138
- case 2 : return Color . FromArgb ( 255 , p , v , t ) ;
139
- case 3 : return Color . FromArgb ( 255 , p , q , v ) ;
140
- case 4 : return Color . FromArgb ( 255 , t , p , v ) ;
141
- default : return Color . FromArgb ( 255 , v , p , q ) ;
138
+ case 0 : return new SKColor ( v , t , p , Alpha ) ;
139
+ case 1 : return new SKColor ( q , v , p , Alpha ) ;
140
+ case 2 : return new SKColor ( p , v , t , Alpha ) ;
141
+ case 3 : return new SKColor ( p , q , v , Alpha ) ;
142
+ case 4 : return new SKColor ( t , p , v , Alpha ) ;
143
+ default : return new SKColor ( v , p , q , Alpha ) ;
142
144
}
143
145
}
144
146
0 commit comments