Skip to content

Instantly share code, notes, and snippets.

@mlhaufe
Last active November 17, 2016 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlhaufe/e8c0e0602a81903deaed44f33a65dbed to your computer and use it in GitHub Desktop.
Save mlhaufe/e8c0e0602a81903deaed44f33a65dbed to your computer and use it in GitHub Desktop.
simple color shading
using System;
public class Program
{
public static void Main()
{
Func<string,double,string> shade = (hex, pct) => {
Func<int,int,int,int> clamp =
(value,min,max) => value < min ? min : value > max ? max : value;
var num = Convert.ToUInt32(hex.Replace("#",""),16);
int amt = (int)Math.Round(2.55 * pct);
int R = (int)((num >> 16) + amt);
var G = (int)((num >> 8 & 0x00FF) + amt);
var B = (int)((num & 0x0000FF) + amt);
var C = (0x1000000 + (clamp(R,0,0xFF) * 0x10000 + clamp(G,0,0xFF)*0x100 + clamp(B,0,0xFF))).ToString("X");
return "#" + C.Substring(1,C.Length - 1);
};
Console.WriteLine(shade("#FF00FF",100));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment