public static Value Saturate(IEnumerable <Value> parameters, IPosition position) { if (parameters.Count() != 2) { Current.RecordError(ErrorType.Compiler, position, "Saturate expects a color parameter, and a percentage"); return(ExcludeFromOutputValue.Singleton); } var color = parameters.ElementAt(0); var percent = parameters.ElementAt(1); if (!(color is ColorValue)) { Current.RecordError(ErrorType.Compiler, position, "Saturate expects a color as its first parameter, found [" + color + "]"); return(ExcludeFromOutputValue.Singleton); } if (!(percent is NumberWithUnitValue)) { Current.RecordError(ErrorType.Compiler, position, "Saturate expects a percentage as its second parameter, found [" + percent + "]"); return(ExcludeFromOutputValue.Singleton); } var colorV = (ColorValue)color; var percentV = (NumberWithUnitValue)percent; if (percentV.Unit != Unit.Percent) { Current.RecordError(ErrorType.Compiler, position, "Saturate expects a percentage as its second parameter, found [" + percent + "]"); return(ExcludeFromOutputValue.Singleton); } var hsl = ConvertToHSL(colorV, position); if (hsl == null) { return(ExcludeFromOutputValue.Singleton); } var h = new NumberValue(hsl.Item1); var l = new NumberWithUnitValue(hsl.Item3 * 100m, Unit.Percent); var s = decimal.Round(hsl.Item2 + percentV.Value / 100m, 2); if (s > 1m) { s = 1m; } return(new HSLColorValue(h, new NumberWithUnitValue(s * 100m, Unit.Percent), l)); }
private static void TryMinifyNumberWithUnit(NumberWithUnitValue value, ReadOnlyDictionary <Unit, decimal> possibleConversions, ref NumberWithUnitValue smallest) { if (!possibleConversions.ContainsKey(value.Unit)) { return; } var min = MinifyNumberValue(value); var ret = new NumberWithUnitValue(min.Value, value.Unit); string retStr; using (var buffer = new StringWriter()) { ret.Write(buffer); retStr = buffer.ToString(); } var inBasic = min.Value * possibleConversions[value.Unit]; foreach (var unit in possibleConversions.Keys) { var conversion = possibleConversions[unit]; var maxPrecision = inBasic / conversion; // 5 decimal points seems like an acceptable level of precision; webkit seems to agree var inUnit = decimal.Round(maxPrecision, 5); var asNum = new NumberValue(inUnit); var minified = MinifyNumberValue(asNum); var newMin = new NumberWithUnitValue(minified.Value, unit); string newMinStr; using (var buffer = new StringWriter()) { newMin.Write(buffer); newMinStr = buffer.ToString(); } if (newMinStr.Length < retStr.Length) { ret = newMin; retStr = newMinStr; } } smallest = ret; }
public static Value Spin(IEnumerable <Value> parameters, IPosition position) { if (parameters.Count() != 2) { Current.RecordError(ErrorType.Compiler, position, "Spin expects a color parameter, and a unit-less number"); return(ExcludeFromOutputValue.Singleton); } var color = parameters.ElementAt(0); var number = parameters.ElementAt(1); if (!(color is ColorValue)) { Current.RecordError(ErrorType.Compiler, position, "Spin expects a color as its first parameter, found [" + color + "]"); return(ExcludeFromOutputValue.Singleton); } if (!(number is NumberValue) || (number is NumberWithUnitValue)) { Current.RecordError(ErrorType.Compiler, position, "Spin expects a unit-less number as its second parameter, found [" + number + "]"); return(ExcludeFromOutputValue.Singleton); } var colorV = (ColorValue)color; var numberV = (NumberValue)number; var parts = ConvertToHSL(colorV, position); if (parts == null) { return(ExcludeFromOutputValue.Singleton); } var h = (int)(parts.Item1 + numberV.Value); h = ((h % 360) + 360) % 360; var s = new NumberWithUnitValue(parts.Item2 * 100m, Unit.Percent); var l = new NumberWithUnitValue(parts.Item3 * 100m, Unit.Percent); if (colorV is RGBAColorValue) { var hsl = new HSLColorValue(new NumberValue(h), s, l); var rgbParts = ConvertToRGB(hsl, position); return(new RGBAColorValue(new NumberValue(rgbParts.Item1), new NumberValue(rgbParts.Item2), new NumberValue(rgbParts.Item3), ((RGBAColorValue)colorV).Alpha)); } return(new HSLColorValue(new NumberValue(h), s, l)); }