Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Drawing;
- using System.Numerics;
- namespace ConsoleApp1
- {
- class ColourCalculator
- {
- public double width;
- public double height;
- public double maxMag;
- static private int MAX = 255;
- public ColourCalculator(double width, double height, double maxMag = 0)
- {
- this.width = width;
- this.height = height;
- this.maxMag = maxMag;
- }
- public Color CalculateColor(int result, int itmax, bool BW = true)
- {
- if (result > itmax)
- {
- result = itmax;
- } else if (result < 0) result = 0;
- if (BW)
- {
- int gradient = result * (MAX / itmax);
- if (gradient > MAX) gradient = MAX;
- return Color.FromArgb(gradient, gradient, gradient);
- }
- else
- {
- int gradient = result * (3 * MAX / itmax);
- if (gradient >= 3 * MAX) gradient = 3 * MAX - 1;
- if (gradient >= 0 && gradient < MAX)
- {
- return Color.FromArgb(MAX - gradient, gradient, 0);
- }
- else if (gradient >= MAX && gradient < 2 * MAX)
- {
- gradient -= MAX;
- return Color.FromArgb(0, MAX - gradient, gradient);
- }
- else
- {
- gradient -= 2 * MAX;
- return Color.FromArgb(gradient, 0, MAX - gradient);
- }
- }
- }
- public Color CalculateColor(Complex result, bool shade)
- {
- if (result == 0)
- {
- return Color.Black;
- }
- double angle = result.Phase * 180/(Math.PI);
- if (angle < 0)
- {
- angle += 360;
- }
- angle = angle % 360;
- double magnitude = CalculateMagnitude(result);
- if (double.IsNaN(angle) || double.IsNaN(magnitude)) return Color.White;
- int region = CalculateRegion(angle);
- if (shade)
- {
- return CalculateAbsoluteColor(angle, region, magnitude);
- }
- else
- {
- return CalculateAbsoluteColor(angle, region);
- }
- }
- private double CalculateMagnitude(Complex result)
- {
- double rawMag = result.Magnitude;
- if (rawMag >= maxMag)
- {
- return 1;
- }
- else
- {
- return rawMag / maxMag;
- }
- }
- private Color CalculateAbsoluteColor(double angle, int region, double magnitudeFrac = -1)
- {
- region = region % 3;
- angle -= 120 * region;
- magnitudeFrac = (magnitudeFrac < 0) ? 1 : CalculateMagnitudeFrac(magnitudeFrac);
- double tempresult_0 = (angle / 120) * MAX * magnitudeFrac;
- double tempresult_1 = Math.Round(tempresult_0);
- int infusion = Convert.ToInt32(tempresult_1);
- int newMax = Convert.ToInt32(Math.Round(MAX * magnitudeFrac));
- Color color;
- try
- {
- if (region == 0)
- {
- color = Color.FromArgb(newMax - infusion, infusion, 0);
- }
- else if (region == 1)
- {
- color = Color.FromArgb(0, newMax - infusion, infusion);
- }
- else
- {
- color = Color.FromArgb(infusion, 0, newMax - infusion);
- }
- return color;
- }
- catch (Exception e)
- {
- throw e;
- }
- }
- private double CalculateMagnitudeFrac(double mag)
- {
- return Math.Sqrt(1 - Math.Pow(mag - 1, 128));
- }
- private int CalculateRegion(double angle)
- {
- if (angle >= 0 && angle < 120)
- {
- return 0;
- }
- else if (angle >= 120 && angle < 240)
- {
- return 1;
- }
- else
- {
- return 2;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment