private bool TryReplaceAsHSV(IColorElement colorElement)
        {
            // Only do this if we've already got a call to HSVToRGB
            var invocationExpression = myOwningExpression as IInvocationExpression;
            if (invocationExpression == null || invocationExpression.Reference?.GetName() != "HSVToRGB" ||
                invocationExpression.Arguments.Count < 3)
            {
                return false;
            }

            var newColor = colorElement.RGBColor;
            float h, s, v;
            ColorUtils.ColorToHSV(newColor, out h, out s, out v);

            // Round to 2 decimal places to match the values shown in the colour palette quick fix
            h = (float) Math.Round(h, 2);
            s = (float) Math.Round(s, 2);
            v = (float) Math.Round(v, 2);

            var module = myOwningExpression.GetPsiModule();
            var elementFactory = CSharpElementFactory.GetInstance(Owner);

            // ReSharper disable AssignNullToNotNullAttribute
            var arguments = invocationExpression.Arguments;
            arguments[0].Value.ReplaceBy(elementFactory.CreateExpressionByConstantValue(new ConstantValue(h, module)));
            arguments[1].Value.ReplaceBy(elementFactory.CreateExpressionByConstantValue(new ConstantValue(s, module)));
            arguments[2].Value.ReplaceBy(elementFactory.CreateExpressionByConstantValue(new ConstantValue(v, module)));
            // ReSharper restore AssignNullToNotNullAttribute

            return true;
        }
        private bool TryReplaceAsHSV(IColorElement colorElement)
        {
            // Only do this if we've already got a call to HSVToRGB
            var invocationExpression = myOwningExpression as IInvocationExpression;

            if (invocationExpression == null || invocationExpression.Reference?.GetName() != "HSVToRGB" ||
                invocationExpression.Arguments.Count < 3)
            {
                return(false);
            }

            var   newColor = colorElement.RGBColor;
            float h, s, v;

            ColorUtils.ColorToHSV(newColor, out h, out s, out v);

            // Round to 2 decimal places to match the values shown in the colour palette quick fix
            h = (float)Math.Round(h, 2);
            s = (float)Math.Round(s, 2);
            v = (float)Math.Round(v, 2);

            var module         = myOwningExpression.GetPsiModule();
            var elementFactory = CSharpElementFactory.GetInstance(Owner);

            // ReSharper disable AssignNullToNotNullAttribute
            var arguments = invocationExpression.Arguments;

            arguments[0].Value.ReplaceBy(elementFactory.CreateExpressionByConstantValue(new ConstantValue(h, module)));
            arguments[1].Value.ReplaceBy(elementFactory.CreateExpressionByConstantValue(new ConstantValue(s, module)));
            arguments[2].Value.ReplaceBy(elementFactory.CreateExpressionByConstantValue(new ConstantValue(v, module)));
            // ReSharper restore AssignNullToNotNullAttribute

            return(true);
        }
Пример #3
0
 private static string GetFormattedString(IColorElement colorElement)
 {
     return(string.Format(CultureInfo.InvariantCulture,
                          "({0:0.##}, {1:0.##}, {2:0.##}, {3:0.##})",
                          colorElement.RGBColor.R / 255.0,
                          colorElement.RGBColor.G / 255.0,
                          colorElement.RGBColor.B / 255.0,
                          colorElement.RGBColor.A / 255.0));
 }
        public void Bind(IColorElement colorElement)
        {
            if (TryReplaceAsNamedColor(colorElement))
                return;

            if (TryReplaceAsHSV(colorElement))
                return;

            TryReplaceAsConstructor(colorElement);
        }
        public void Bind(IColorElement colorElement)
        {
            var languageService = ShaderLabLanguage.Instance.LanguageService();

            Assertion.AssertNotNull(languageService, "languageService != null");
            var formattedString = GetFormattedString(colorElement);
            var lexer           = languageService.GetPrimaryLexerFactory().CreateLexer(new StringBuffer(formattedString));
            var parser          = (IShaderLabParser)languageService.CreateParser(lexer, null, null);
            var newLiteral      = parser.ParseColorLiteral();

            myColorPropertyValue?.SetColor(newLiteral);
            myColorValue?.SetConstant(newLiteral);
        }
Пример #6
0
        public void Bind(IColorElement colorElement)
        {
            var languageService = ShaderLabLanguage.Instance.LanguageService();

            Assertion.AssertNotNull(languageService, "languageService != null");
            var lexer = languageService.GetPrimaryLexerFactory().CreateLexer(new StringBuffer(
                                                                                 $"({colorElement.RGBColor.R/255.0:0.##}, {colorElement.RGBColor.G/255.0:0.##}, {colorElement.RGBColor.B/255.0:0.##}, {colorElement.RGBColor.A/255.0:0.##})"));
            var parser     = (IShaderLabParser)languageService.CreateParser(lexer, null, null);
            var newLiteral = parser.ParseColorLiteral();

            myColorPropertyValue?.SetColor(newLiteral);
            myColorValue?.SetConstant(newLiteral);
        }
        public UnityColorReference(IColorElement colorElement, IExpression owningExpression, ITreeNode owner,
            DocumentRange colorConstantRange)
        {
            myOwningExpression = owningExpression;
            ColorElement = colorElement;
            Owner = owner;
            ColorConstantRange = colorConstantRange;

            BindOptions = new ColorBindOptions
            {
                BindsToName = true,
                BindsToValue = true
            };
        }
        public void Bind(IColorElement colorElement)
        {
            if (TryReplaceAsNamedColor(colorElement))
            {
                return;
            }

            if (TryReplaceAsHSV(colorElement))
            {
                return;
            }

            TryReplaceAsConstructor(colorElement);
        }
        public UnityColorReference(IColorElement colorElement, IExpression owningExpression, ITreeNode owner,
                                   DocumentRange colorConstantRange)
        {
            myOwningExpression = owningExpression;
            ColorElement       = colorElement;
            Owner = owner;
            ColorConstantRange = colorConstantRange;

            BindOptions = new ColorBindOptions
            {
                BindsToName  = true,
                BindsToValue = true
            };
        }
Пример #10
0
        public ShaderLabColorReference(IColorElement colorElement, IColorPropertyValue colorPropertyValue, IColorValue colorValue, DocumentRange colorConstantRange)
        {
            myColorPropertyValue = colorPropertyValue;
            myColorValue         = colorValue;
            ColorElement         = colorElement;
            Owner = (ITreeNode)colorValue ?? colorPropertyValue;
            ColorConstantRange = colorConstantRange;

            BindOptions = new ColorBindOptions
            {
                BindsToName  = false,
                BindsToValue = true
            };
        }
        private bool TryReplaceAsNamedColor(IColorElement colorElement)
        {
            var colorType = GetColorType();

            var newColor = UnityColorTypes.PropertyFromColorElement(colorType, colorElement,
                myOwningExpression.GetPsiModule());
            if (newColor == null) return false;

            var newExp = CSharpElementFactory.GetInstance(Owner)
                .CreateExpression("$0.$1", newColor.Value.First, newColor.Value.Second);

            var oldExp = myOwningExpression as ICSharpExpression;
            return oldExp?.ReplaceBy(newExp) != null;
        }
Пример #12
0
        public static Pair<ITypeElement, ITypeMember>? PropertyFromColorElement(ITypeElement qualifierType, IColorElement colorElement, IPsiModule module)
        {
            var colorName = UnityNamedColors.GetColorName(colorElement.RGBColor);
            if (string.IsNullOrEmpty(colorName))
                return null;

            var unityColorType = GetInstance(module).UnityColorType;
            if (unityColorType == null || !unityColorType.Equals(qualifierType)) return null;

            var colorProperties = GetStaticColorProperties(unityColorType);
            var propertyTypeMember = colorProperties.FirstOrDefault(p => p.ShortName == colorName);
            if (propertyTypeMember == null) return null;

            return Pair.Of(unityColorType, propertyTypeMember);
        }
        private bool TryReplaceAsNamedColor(IColorElement colorElement)
        {
            var colorType = GetColorType();

            var newColor = UnityColorTypes.PropertyFromColorElement(colorType, colorElement,
                                                                    myOwningExpression.GetPsiModule());

            if (newColor == null)
            {
                return(false);
            }

            var newExp = CSharpElementFactory.GetInstance(Owner)
                         .CreateExpression("$0.$1", newColor.Value.First, newColor.Value.Second);

            var oldExp = myOwningExpression as ICSharpExpression;

            return(oldExp?.ReplaceBy(newExp) != null);
        }
Пример #14
0
 private void RefreshValue(ColorElementViewModel vm, IColorElement ce)
 {
     vm.CPBViewModel.CurrentColor = ce.GetColor();
 }
        private void TryReplaceAsConstructor(IColorElement colorElement)
        {
            // TODO: Perhaps we should try and update an existing constructor?
            var newColor = colorElement.RGBColor;

            var colorType = GetColorType();
            if (colorType == null) return;

            var elementFactory = CSharpElementFactory.GetInstance(Owner);
            var module = myOwningExpression.GetPsiModule();
            var unityColorTypes = UnityColorTypes.GetInstance(module);

            var requiresAlpha = newColor.A != byte.MaxValue;

            ConstantValue r, g, b, a;
            if (unityColorTypes.UnityColorType != null && unityColorTypes.UnityColorType.Equals(colorType))
            {
                // Round to 2 decimal places, to match the values shown in the colour palette quick fix
                r = new ConstantValue((float) Math.Round(newColor.R / 255.0, 2), module);
                g = new ConstantValue((float) Math.Round(newColor.G / 255.0, 2), module);
                b = new ConstantValue((float) Math.Round(newColor.B / 255.0, 2), module);
                a = new ConstantValue((float) Math.Round(newColor.A / 255.0, 2), module);
            }
            else if (unityColorTypes.UnityColor32Type != null && unityColorTypes.UnityColor32Type.Equals(colorType))
            {
                // ReSharper formats byte constants with an explicit cast
                r = new ConstantValue((int)newColor.R, module);
                g = new ConstantValue((int)newColor.G, module);
                b = new ConstantValue((int)newColor.B, module);
                a = new ConstantValue((int)newColor.A, module);

                requiresAlpha = true;
            }
            else
                return;

            ICSharpExpression newExp;
            if (!requiresAlpha)
            {
                newExp = elementFactory
                    .CreateExpression("new $0($1, $2, $3)", colorType,
                        elementFactory.CreateExpressionByConstantValue(r),
                        elementFactory.CreateExpressionByConstantValue(g),
                        elementFactory.CreateExpressionByConstantValue(b));
            }
            else
            {
                newExp = elementFactory
                    .CreateExpression("new $0($1, $2, $3, $4)", colorType,
                        elementFactory.CreateExpressionByConstantValue(r),
                        elementFactory.CreateExpressionByConstantValue(g),
                        elementFactory.CreateExpressionByConstantValue(b),
                        elementFactory.CreateExpressionByConstantValue(a));
            }

            var oldExp = (ICSharpExpression) myOwningExpression;
            oldExp.ReplaceBy(newExp);
        }
Пример #16
0
        public static Pair <ITypeElement, ITypeMember>?PropertyFromColorElement(ITypeElement qualifierType, IColorElement colorElement, IPsiModule module)
        {
            var colorName = UnityNamedColors.GetColorName(colorElement.RGBColor);

            if (string.IsNullOrEmpty(colorName))
            {
                return(null);
            }

            var unityColorType = GetInstance(module).UnityColorType;

            if (unityColorType == null || !unityColorType.Equals(qualifierType))
            {
                return(null);
            }

            var colorProperties    = GetStaticColorProperties(unityColorType);
            var propertyTypeMember = colorProperties.FirstOrDefault(p => p.ShortName == colorName);

            if (propertyTypeMember == null)
            {
                return(null);
            }

            return(Pair.Of(unityColorType, propertyTypeMember));
        }
Пример #17
0
 private void RefreshValue(ColorElementViewModel vm, IColorElement ce)
 {
     vm.CPBViewModel.CurrentColor = ce.GetColor();
 }
        private void TryReplaceAsConstructor(IColorElement colorElement)
        {
            // TODO: Perhaps we should try and update an existing constructor?
            var newColor = colorElement.RGBColor;

            var colorType = GetColorType();

            if (colorType == null)
            {
                return;
            }

            var elementFactory  = CSharpElementFactory.GetInstance(Owner);
            var module          = myOwningExpression.GetPsiModule();
            var unityColorTypes = UnityColorTypes.GetInstance(module);

            var requiresAlpha = newColor.A != byte.MaxValue;

            ConstantValue r, g, b, a;

            if (unityColorTypes.UnityColorType != null && unityColorTypes.UnityColorType.Equals(colorType))
            {
                // Round to 2 decimal places, to match the values shown in the colour palette quick fix
                r = new ConstantValue((float)Math.Round(newColor.R / 255.0, 2), module);
                g = new ConstantValue((float)Math.Round(newColor.G / 255.0, 2), module);
                b = new ConstantValue((float)Math.Round(newColor.B / 255.0, 2), module);
                a = new ConstantValue((float)Math.Round(newColor.A / 255.0, 2), module);
            }
            else if (unityColorTypes.UnityColor32Type != null && unityColorTypes.UnityColor32Type.Equals(colorType))
            {
                // ReSharper formats byte constants with an explicit cast
                r = new ConstantValue((int)newColor.R, module);
                g = new ConstantValue((int)newColor.G, module);
                b = new ConstantValue((int)newColor.B, module);
                a = new ConstantValue((int)newColor.A, module);

                requiresAlpha = true;
            }
            else
            {
                return;
            }

            ICSharpExpression newExp;

            if (!requiresAlpha)
            {
                newExp = elementFactory
                         .CreateExpression("new $0($1, $2, $3)", colorType,
                                           elementFactory.CreateExpressionByConstantValue(r),
                                           elementFactory.CreateExpressionByConstantValue(g),
                                           elementFactory.CreateExpressionByConstantValue(b));
            }
            else
            {
                newExp = elementFactory
                         .CreateExpression("new $0($1, $2, $3, $4)", colorType,
                                           elementFactory.CreateExpressionByConstantValue(r),
                                           elementFactory.CreateExpressionByConstantValue(g),
                                           elementFactory.CreateExpressionByConstantValue(b),
                                           elementFactory.CreateExpressionByConstantValue(a));
            }

            var oldExp = (ICSharpExpression)myOwningExpression;

            oldExp.ReplaceBy(newExp);
        }