Beispiel #1
0
        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);
        }
Beispiel #2
0
        public static Value Darken(IEnumerable<Value> parameters, IPosition position)
        {
            if (parameters.Count() != 2)
            {
                Current.RecordError(ErrorType.Compiler, position, "Darken 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, "Darken expects a color as its first parameter, found [" + color + "]");
                return ExcludeFromOutputValue.Singleton;
            }

            if (!(percent is NumberWithUnitValue))
            {
                Current.RecordError(ErrorType.Compiler, position, "Darken 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, "Darken 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 s = new NumberWithUnitValue(hsl.Item2 * 100m, Unit.Percent);

            var l = decimal.Round(hsl.Item3 - percentV.Value / 100m, 2);
            if (l < 0m) l = 0m;

            return new HSLColorValue(h, s, new NumberWithUnitValue(l * 100m, Unit.Percent));
        }
Beispiel #3
0
        public static Value FadeOut(IEnumerable<Value> parameters, IPosition position)
        {
            if (parameters.Count() != 2)
            {
                Current.RecordError(ErrorType.Compiler, position, "FadeOut 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, "FadeOut expects a color as its first parameter, found [" + color + "]");
                return ExcludeFromOutputValue.Singleton;
            }

            if (!(percent is NumberWithUnitValue))
            {
                Current.RecordError(ErrorType.Compiler, position, "FadeOut 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, "FadeOut expects a percentage as its second parameter, found [" + percent + "]");
                return ExcludeFromOutputValue.Singleton;
            }

            var alpha = new NumberWithUnitValue(100, Unit.Percent);
            if (colorV is RGBAColorValue)
            {
                var a = ((RGBAColorValue)colorV).Alpha;
                if (a is NumberWithUnitValue)
                {
                    alpha = (NumberWithUnitValue)a;
                }
                else
                {
                    alpha = new NumberWithUnitValue((100m * ((NumberValue)a).Value), Unit.Percent);
                }
            }

            var parts = ConvertToRGB(colorV, position);

            var newAlpha = alpha.Value - percentV.Value;
            if (newAlpha < 0m) newAlpha = 0m;

            newAlpha /= 100m;

            return new RGBAColorValue(new NumberValue(parts.Item1), new NumberValue(parts.Item2), new NumberValue(parts.Item3), new NumberValue(newAlpha));
        }
Beispiel #4
0
        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;
        }
Beispiel #5
0
        private static NumberWithUnitValue MinifyNumberWithUnit(NumberWithUnitValue value)
        {
            var ret = value;

            TryMinifyNumberWithUnit(value, Value.ConvertableSizeUnits, ref ret);
            TryMinifyNumberWithUnit(value, Value.ConvertableTimeUnits, ref ret);
            TryMinifyNumberWithUnit(value, Value.ConvertableResolutionUnits, ref ret);
            TryMinifyNumberWithUnit(value, Value.ConvertableAngleUnits, ref ret);
            TryMinifyNumberWithUnit(value, Value.ConvertableFrequencyUnits, ref ret);

            return ret;
        }
Beispiel #6
0
        private static NumberWithUnitValue MinifyNumberWithUnit(NumberWithUnitValue value)
        {
            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();
            }

            if (Value.ConvertableSizeUnits.ContainsKey(value.Unit))
            {
                var inMM = min.Value * Value.ConvertableSizeUnits[value.Unit];

                foreach (var unit in Value.ConvertableSizeUnits.Keys)
                {
                    var inUnit = inMM / Value.ConvertableSizeUnits[unit];

                    var newMin = new NumberWithUnitValue(MinifyNumberValue(new NumberValue(inUnit)).Value, unit);
                    string newMinStr;

                    using (var buffer = new StringWriter())
                    {
                        newMin.Write(buffer);
                        newMinStr = buffer.ToString();
                    }

                    if (newMinStr.Length < retStr.Length)
                    {
                        ret = newMin;
                        retStr = newMinStr;
                    }
                }
            }

            if (Value.ConvertableTimeUnits.ContainsKey(value.Unit))
            {
                var inS = min.Value * Value.ConvertableTimeUnits[value.Unit];

                foreach (var unit in Value.ConvertableTimeUnits.Keys)
                {
                    var inUnit = inS / Value.ConvertableTimeUnits[unit];
                    var newMin = new NumberWithUnitValue(MinifyNumberValue(new NumberValue(inUnit)).Value, unit);
                    string newMinStr;

                    using (var buffer = new StringWriter())
                    {
                        newMin.Write(buffer);
                        newMinStr = buffer.ToString();
                    }

                    if (newMinStr.Length < retStr.Length)
                    {
                        ret = newMin;
                        retStr = newMinStr;
                    }
                }
            }

            if (Value.ConvertableResolutionUnits.ContainsKey(value.Unit))
            {
                var inDpcm = min.Value * Value.ConvertableResolutionUnits[value.Unit];

                foreach (var unit in Value.ConvertableResolutionUnits.Keys)
                {
                    var inUnit = inDpcm / Value.ConvertableResolutionUnits[unit];
                    var newMin = new NumberWithUnitValue(MinifyNumberValue(new NumberValue(inUnit)).Value, unit);
                    string newMinStr;

                    using (var buffer = new StringWriter())
                    {
                        newMin.Write(buffer);
                        newMinStr = buffer.ToString();
                    }

                    if (newMinStr.Length < retStr.Length)
                    {
                        ret = newMin;
                        retStr = newMinStr;
                    }
                }
            }

            return ret;
        }