Last active
November 17, 2016 19:35
-
-
Save mlhaufe/e8c0e0602a81903deaed44f33a65dbed to your computer and use it in GitHub Desktop.
simple color shading
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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