Support color data types

Topics: New Features
Mar 4, 2014 at 8:32 PM
Edited Mar 4, 2014 at 8:40 PM
Since System.Drawing.Color and System.Windows.Media.Color both have arithmetic operators defined, please consider adding it to build in datatypes; so we can evaluate expressions like #fff * #eee - (#ddee55 / #911222). The color codes and color names tokens can be resolved to RGB using System.Drawing.ColorTranslator.FromHtml("lightblue").

The interoperability between System.Windows.Media.Color and System.Drawing.Color is safely possible via RGB.

More complicated expression would be if it involve numeric, e.g. #fffddd - 5, which should subtract 5 from all three tones individually Color.R - 5, Color.G - 5 and Color.B = 5.

Currently, the operator doesn't support numeric signatures:

Without having those mixed operators, one approach could be to normalize all tokens to hex triplet #XXXXX or #XXX if applicable:
using System.Drawing;

var color = ColorTranslator.FromHtml(token);

if(color == null)
    int num;
    if(!int.TryParse(token, out num))
        return null; // the token is not a number

    // Each channel shouldn't be greater than FF (255)
    num = (num > 255) ? 255 : num; 

                 // will convert to short code as well (e.g. #fff),
                 // or X2 for strictly six digits (#ffffff)
    var str = num.ToString("X");
    str = string.Format(CultureInfo.CurrentCulture, "#{0}{0}{0}", str);

    color = ColorTranslator.FromHtml(str);                  

if(color == null)
    return null; // The token is neither number nor known color
// Now do something with the normalized to color token
OAN, is this project dead? No release since 2011 .... on CodePlex or Nuget.