-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFractalColorizer.cs
More file actions
53 lines (47 loc) · 1.69 KB
/
FractalColorizer.cs
File metadata and controls
53 lines (47 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using Avalonia.Media;
using System;
namespace Fractals
{
/// <summary>
/// Gestion des couleurs pour le rendu des fractales
/// </summary>
public static class FractalColorizer
{
/// <summary>
/// Calcule la couleur d'un pixel en fonction du nombre d'itérations
/// </summary>
public static Color GetColor(int iter, int maxIter, double lastX, double lastY)
{
if (iter == maxIter)
return Colors.Black;
// Smooth coloring avec les dernières valeurs de x et y
double zn = Math.Sqrt(lastX * lastX + lastY * lastY);
double smooth = iter + 1 - Math.Log(Math.Log(zn)) / Math.Log(2.0);
double hue = 360.0 * (smooth / maxIter * 3.0) % 360.0;
return HsvToColor(hue, 0.8, 1.0);
}
/// <summary>
/// Convertit une couleur HSV en RGB
/// </summary>
private static Color HsvToColor(double h, double s, double v)
{
h = h % 360;
int hi = (int)(h / 60) % 6;
double f = h / 60 - Math.Floor(h / 60);
v = v * 255;
byte p = (byte)(v * (1 - s));
byte q = (byte)(v * (1 - f * s));
byte t = (byte)(v * (1 - (1 - f) * s));
byte vb = (byte)v;
return hi switch
{
0 => Color.FromArgb(255, vb, t, p),
1 => Color.FromArgb(255, q, vb, p),
2 => Color.FromArgb(255, p, vb, t),
3 => Color.FromArgb(255, p, q, vb),
4 => Color.FromArgb(255, t, p, vb),
_ => Color.FromArgb(255, vb, p, q)
};
}
}
}