Results 1 to 2 of 2
  1. #1
    Zaczero's Avatar
    Join Date
    Oct 2013
    Gender
    male
    Location
    localhost
    Posts
    3,288
    Reputation
    1517
    Thanks
    14,262
    My Mood
    Angelic

    C# Simple Gradient Class


    Example usage:
    Code:
    using Zaczero;
    (..)
    
    var gradient = new Gradient();
    
    gradient.Add(new GradientColor(0.0, "#ED4264"));
    gradient.Add(new GradientColor(0.4, "#4286fF"));
    gradient.Add(new GradientColor(1.0, "#FFEDBC"));
    
    var coolColor = gradient.Calculate(0.65);
    OFFSET RANGE: (double) 0.000 - 1.000


    Source code:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    
    namespace Zaczero
    {
        public struct GradientColor : IComparable<GradientColor>
        {
            public double Offset;
    
            public double R;
            public double G;
            public double B;
            public double A;
    
            public GradientColor(double offset, double r, double g, double b, double a)
            {
                Offset = offset;
    
                R = r;
                G = g;
                B = b;
                A = a;
            }
    
            public GradientColor(double offset, double r, double g, double b)
            {
                Offset = offset;
    
                R = r;
                G = g;
                B = b;
                A = 1d;
            }
    
            public GradientColor(double offset, string code)
            {
                const string invalidColorCode = "Invalid color code";
    
                Offset = offset;
    
                code = code.TrimStart('#');
    
                foreach (var c in code)
                {
                    if (!char.IsLetterOrDigit(c))
                    {
                        throw new ArgumentException(invalidColorCode);
                    }
                }
    
                if (code.Length == 6)
                {
                    code += "FF";
                }
    
                if (code.Length == 8)
                {
                    var r = code.Substring(0, 2);
                    var g = code.Substring(2, 2);
                    var b = code.Substring(4, 2);
                    var a = code.Substring(6, 2);
    
                    R = byte.Parse(r, NumberStyles.HexNumber) / 255d;
                    G = byte.Parse(g, NumberStyles.HexNumber) / 255d;
                    B = byte.Parse(b, NumberStyles.HexNumber) / 255d;
                    A = byte.Parse(a, NumberStyles.HexNumber) / 255d;
                }
                else
                {
                    throw new ArgumentException(invalidColorCode);
                }
            }
    
            public int CompareTo(GradientColor other)
            {
                return Offse*****mpareTo(other.Offset);
            }
        }
    
        public class Gradient
        {
            private readonly List<GradientColor> _colors;
    
            public Gradient()
            {
                _colors = new List<GradientColor>();
            }
    
            public void Add(GradientColor color)
            {
                _colors.Add(color);
                _colors.Sort();
            }
    
            public GradientColor Calculate(double offset)
            {
                var colorStart = default(GradientColor);
                var colorEnd = default(GradientColor);
    
                for (var i = 1; i < _colors.Count; i++)
                {
                    if (_colors[i].Offset > offset)
                    {
                        colorStart = _colors[i - 1];
                        colorEnd = _colors[i];
                        break;
                    }
                }
    
                var offsetRange = colorEnd.Offset - colorStart.Offset;
    
                var offsetPercentage = (offset - colorStart.Offset) / offsetRange;
                var offsetPercentageInv = 1 - offsetPercentage;
    
                return new GradientColor(offset,
                    colorStart.R * offsetPercentageInv + colorEnd.R * offsetPercentage,
                    colorStart.G * offsetPercentageInv + colorEnd.G * offsetPercentage,
                    colorStart.B * offsetPercentageInv + colorEnd.B * offsetPercentage,
                    colorStart.A * offsetPercentageInv + colorEnd.A * offsetPercentage);
            }
        }
    }

     
    That gradient on my thread got generated using this class
    . . . malsignature.com . . .



    [ global rules ] [ scam report ] [ image title ] [ name change ] [ anime force ]
    [ league of legends marketplace rules ] [ battlefield marketplace rules ]

    "because everytime you post a picture of anime in here
    your virginity's time increases by 1 month"
    ~Smoke 2/18/2018


    Former Staff 09-29-2018
    Battlefield Minion 07-21-2018
    Premium Seller 03-04-2018
    Publicist 12-10-2017
    League of Legends Minion 05-31-2017
    Premium 02-05-2017
    Member 10-13-2013

  2. The Following 3 Users Say Thank You to Zaczero For This Useful Post:

    KMWTW (06-02-2017),Silent (06-03-2017),Steve (06-26-2017)

  3. #2
    KMWTW's Avatar
    Join Date
    Nov 2015
    Gender
    male
    Location
    www.learncpp.com
    Posts
    2,466
    Reputation
    321
    Thanks
    14,450
    My Mood
    Blah
    Nice release! I hope it will be rly useful <3

  4. The Following User Says Thank You to KMWTW For This Useful Post:

    Zaczero (06-02-2017)

Similar Threads

  1. [Release] Simple random class with one time occurring random numbers!
    By Laslod in forum C# Programming
    Replies: 4
    Last Post: 03-26-2013, 11:04 AM
  2. [Release] Simple WeaponMgr Class
    By luizimloko in forum CrossFire Hack Coding / Programming / Source Code
    Replies: 8
    Last Post: 11-12-2012, 05:48 PM
  3. [Release] A simple Hooking class
    By 258456 in forum CrossFire Hack Coding / Programming / Source Code
    Replies: 13
    Last Post: 02-23-2012, 07:37 AM
  4. [Source Code] Simple MemoryMgr Class
    By CodeDemon in forum Combat Arms Hack Coding / Programming / Source Code
    Replies: 21
    Last Post: 02-26-2011, 10:34 PM
  5. [Source]Simple menu class
    By Void in forum C++/C Programming
    Replies: 44
    Last Post: 10-04-2010, 08:21 PM

Tags for this Thread