Guest User

Untitled

a guest
Apr 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.26 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Drawing;
  7. using System.Numerics;
  8.  
  9. namespace ConsoleApp1
  10. {
  11.     class ColourCalculator
  12.     {
  13.         public double width;
  14.         public double height;
  15.         public double maxMag;
  16.         static private int MAX = 255;
  17.  
  18.         public ColourCalculator(double width, double height, double maxMag = 0)
  19.         {
  20.             this.width = width;
  21.             this.height = height;
  22.             this.maxMag = maxMag;
  23.         }
  24.  
  25.         public Color CalculateColor(int result, int itmax, bool BW = true)
  26.         {
  27.             if (result > itmax)
  28.             {
  29.                 result = itmax;
  30.             } else if (result < 0) result = 0;
  31.             if (BW)
  32.             {
  33.                 int gradient = result * (MAX / itmax);
  34.                 if (gradient > MAX) gradient = MAX;
  35.                 return Color.FromArgb(gradient, gradient, gradient);
  36.             }
  37.             else
  38.             {
  39.                 int gradient = result * (3 * MAX / itmax);
  40.                 if (gradient >= 3 * MAX) gradient = 3 * MAX - 1;
  41.                 if (gradient >= 0 && gradient < MAX)
  42.                 {
  43.                     return Color.FromArgb(MAX - gradient, gradient, 0);
  44.                 }
  45.                 else if (gradient >= MAX && gradient < 2 * MAX)
  46.                 {
  47.                     gradient -= MAX;
  48.                     return Color.FromArgb(0, MAX - gradient, gradient);
  49.                 }
  50.                 else
  51.                 {
  52.                     gradient -= 2 * MAX;
  53.                     return Color.FromArgb(gradient, 0, MAX - gradient);
  54.                 }
  55.             }
  56.         }
  57.  
  58.         public Color CalculateColor(Complex result, bool shade)
  59.         {
  60.             if (result == 0)
  61.             {
  62.                 return Color.Black;
  63.             }
  64.             double angle = result.Phase * 180/(Math.PI);
  65.             if (angle < 0)
  66.             {
  67.                 angle += 360;
  68.             }
  69.  
  70.             angle = angle % 360;
  71.             double magnitude = CalculateMagnitude(result);
  72.             if (double.IsNaN(angle) || double.IsNaN(magnitude)) return Color.White;
  73.             int region = CalculateRegion(angle);
  74.             if (shade)
  75.             {
  76.                 return CalculateAbsoluteColor(angle, region, magnitude);
  77.             }
  78.             else
  79.             {
  80.                 return CalculateAbsoluteColor(angle, region);
  81.             }
  82.            
  83.         }
  84.  
  85.         private double CalculateMagnitude(Complex result)
  86.         {
  87.             double rawMag = result.Magnitude;
  88.             if (rawMag >= maxMag)
  89.             {
  90.                 return 1;
  91.             }
  92.             else
  93.             {
  94.                 return rawMag / maxMag;
  95.             }
  96.         }
  97.  
  98.         private Color CalculateAbsoluteColor(double angle, int region, double magnitudeFrac = -1)
  99.         {
  100.             region = region % 3;
  101.             angle -= 120 * region;
  102.             magnitudeFrac = (magnitudeFrac < 0) ? 1 : CalculateMagnitudeFrac(magnitudeFrac);
  103.             double tempresult_0 = (angle / 120) * MAX * magnitudeFrac;
  104.             double tempresult_1 = Math.Round(tempresult_0);
  105.             int infusion = Convert.ToInt32(tempresult_1);
  106.             int newMax = Convert.ToInt32(Math.Round(MAX * magnitudeFrac));
  107.             Color color;
  108.             try
  109.             {
  110.                 if (region == 0)
  111.                 {
  112.                     color = Color.FromArgb(newMax - infusion, infusion, 0);
  113.                 }
  114.                 else if (region == 1)
  115.                 {
  116.                     color = Color.FromArgb(0, newMax - infusion, infusion);
  117.                 }
  118.                 else
  119.                 {
  120.                     color = Color.FromArgb(infusion, 0, newMax - infusion);
  121.                 }
  122.                 return color;
  123.             }
  124.             catch (Exception e)
  125.             {
  126.                 throw e;
  127.             }
  128.         }
  129.  
  130.         private double CalculateMagnitudeFrac(double mag)
  131.         {
  132.             return Math.Sqrt(1 - Math.Pow(mag - 1, 128));
  133.         }
  134.  
  135.         private int CalculateRegion(double angle)
  136.         {
  137.             if (angle >= 0 && angle < 120)
  138.             {
  139.                 return 0;
  140.             }
  141.             else if (angle >= 120 && angle < 240)
  142.             {
  143.                 return 1;
  144.             }
  145.             else
  146.             {
  147.                 return 2;
  148.             }
  149.         }
  150.     }
  151. }
Add Comment
Please, Sign In to add comment