summaryrefslogtreecommitdiff
path: root/java/java-impl/src/com/intellij/codeInsight/daemon/impl/JavaColorProvider.java
blob: dfc7169a27778d9314069ff9a3c32e5497c5651a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Copyright 2000-2012 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.codeInsight.daemon.impl;

import com.intellij.lang.java.JavaLanguage;
import com.intellij.openapi.editor.ElementColorProvider;
import com.intellij.psi.*;
import com.intellij.psi.impl.JavaConstantExpressionEvaluator;
import com.intellij.psi.util.PsiTypesUtil;
import com.intellij.ui.ColorUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.awt.*;

/**
 * @author Konstantin Bulenkov
 */
public class JavaColorProvider implements ElementColorProvider {
  @Override
  public Color getColorFrom(@NotNull PsiElement element) {
    if (element instanceof PsiNewExpression && element.getLanguage() == JavaLanguage.INSTANCE) {
      final PsiNewExpression expr = (PsiNewExpression)element;
      final PsiType type = expr.getType();
      if (type != null) {
        final PsiClass aClass = PsiTypesUtil.getPsiClass(type);
        if (aClass != null) {
          final String fqn = aClass.getQualifiedName();
          if ("java.awt.Color".equals(fqn) || "javax.swing.plaf.ColorUIResource".equals(fqn)) {
            return getColor(expr.getArgumentList());
          }
        }
      }
    }
    return null;
  }

  @Nullable
  private static Color getColor(PsiExpressionList list) {
    try {
      final PsiExpression[] args = list.getExpressions();
      final PsiType[] types = list.getExpressionTypes();
      ColorConstructors type = getConstructorType(types);
      if (type != null) {
        switch (type) {
          case INT:      return new Color(  getInt(args[0]));
          case INT_BOOL: return new Color(  getInt(args[0]), getBoolean(args[1]));
          case INT_x3:   return new Color(  getInt(args[0]),     getInt(args[1]),   getInt(args[2]));
          case INT_x4:   return new Color(  getInt(args[0]),     getInt(args[1]),   getInt(args[2]),   getInt(args[3]));
          case FLOAT_x3: return new Color(getFloat(args[0]),   getFloat(args[1]), getFloat(args[2]));
          case FLOAT_x4: return new Color(getFloat(args[0]),   getFloat(args[1]), getFloat(args[2]), getFloat(args[3]));
        }
      }
    }
    catch (Exception ignore) {
    }
    return null;
  }

  @Nullable
  private static ColorConstructors getConstructorType(PsiType[] types) {
    int len = types.length;
    if (len == 0) return null;

    switch (len) {
      case 1: return ColorConstructors.INT;
      case 2: return ColorConstructors.INT_BOOL;
      case 3: return PsiType.INT.equals(types[0]) ? ColorConstructors.INT_x3 : ColorConstructors.FLOAT_x3;
      case 4: return PsiType.INT.equals(types[0]) ? ColorConstructors.INT_x4 : ColorConstructors.FLOAT_x4;
    }

    return null;
  }

  public static int getInt(PsiExpression expr) {
    return ((Integer)getObject(expr)).intValue();
  }

  public static float getFloat(PsiExpression expr) {
    return ((Float)getObject(expr)).floatValue();
  }

  public static boolean getBoolean(PsiExpression expr) {
    return ((Boolean)getObject(expr)).booleanValue();
  }

  private static Object getObject(PsiExpression expr) {
    return JavaConstantExpressionEvaluator.computeConstantExpression(expr, true);
  }

  @Override
  public void setColorTo(@NotNull PsiElement element, @NotNull Color color) {
    PsiExpressionList argumentList = ((PsiNewExpression)element).getArgumentList();
    assert argumentList != null;
    
    PsiExpression[] expr = argumentList.getExpressions();
    ColorConstructors type = getConstructorType(argumentList.getExpressionTypes());

    assert type != null;

    switch (type) {
      case INT:        
      case INT_BOOL:
        replaceInt(expr[0], color.getRGB(), true);
        return;
      case INT_x3:        
      case INT_x4:
        replaceInt(expr[0], color.getRed());
        replaceInt(expr[1], color.getGreen());
        replaceInt(expr[2], color.getBlue());
        if (type == ColorConstructors.INT_x4) {
          replaceInt(expr[3], color.getAlpha());
        } else if (color.getAlpha() != 255) {
          //todo add alpha
        }
        return;
      case FLOAT_x3:
      case FLOAT_x4:
        float[] rgba = color.getColorComponents(null);
        replaceFloat(expr[0], rgba[0]);
        replaceFloat(expr[1], rgba[1]);
        replaceFloat(expr[2], rgba[2]);
        if (type == ColorConstructors.FLOAT_x4) {
          replaceFloat(expr[3], rgba.length == 4 ? rgba[3] : 0f);
        } else if (color.getAlpha() != 255) {
          //todo add alpha
        }
    }
  }

  private static void replaceInt(PsiExpression expr, int newValue) {
    replaceInt(expr, newValue, false);
  }

  private static void replaceInt(PsiExpression expr, int newValue, boolean hex) {
    PsiElementFactory factory = JavaPsiFacade.getElementFactory(expr.getProject());
    if (getInt(expr) != newValue) {
      String text = hex ? "0x" + ColorUtil.toHex(new Color(newValue)).toUpperCase() : Integer.toString(newValue);
      expr.replace(factory.createExpressionFromText(text, null));
    }
  }
  private static void replaceFloat(PsiExpression expr, float newValue) {
    PsiElementFactory factory = JavaPsiFacade.getElementFactory(expr.getProject());
    if (getFloat(expr) != newValue) {
      expr.replace(factory.createExpressionFromText(String.valueOf(newValue) + "f", null));
    }
  }
  
  private enum ColorConstructors {
    INT, INT_BOOL, INT_x3, INT_x4, FLOAT_x3, FLOAT_x4
  }
}