public IconId GetImageId(IDeclaredElement declaredElement, PsiLanguageType languageType, out bool canApplyExtensions)
        {
            canApplyExtensions = false;

            var typeMember = declaredElement as ITypeMember;

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

            if (!UnityColorTypes.IsColorProperty(typeMember))
            {
                return(null);
            }

            var color = UnityNamedColors.Get(typeMember.ShortName);

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

            return(new ColorIconId(color.Value));
        }
Esempio n. 2
0
        public static UnityColorTypes GetInstance(IPsiModule module)
        {
            var unityColorTypes = module.GetData(ourColorTypesKey);

            if (unityColorTypes == null)
            {
                unityColorTypes = new UnityColorTypes(module);
                module.PutData(ourColorTypesKey, unityColorTypes);
            }

            return(unityColorTypes);
        }
        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);
        }
        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);
        }