2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.intellij
.codeInsight
.javadoc
;
18 import com
.intellij
.psi
.*;
19 import com
.intellij
.util
.ArrayUtil
;
20 import org
.jetbrains
.annotations
.NotNull
;
23 import java
.lang
.reflect
.Field
;
28 public class ColorUtil
{
32 public static String
toHex(@NotNull final Color color
) {
33 final StringBuffer sb
= new StringBuffer();
34 for (int i
= 0; i
< 3; i
++) {
35 String s
= Integer
.toHexString(i
== 0 ? color
.getRed() : i
== 1 ? color
.getGreen() : color
.getBlue());
46 public static String
generatePreviewHtml(@NotNull final Color color
) {
47 return String
.format("<div style=\"width: 50px; height: 30px; background-color: #%s; border: 1px solid #222;\"> </div>", toHex(color
));
50 public static void appendColorPreview(final PsiVariable variable
, final StringBuilder buffer
) {
51 final PsiExpression initializer
= variable
.getInitializer();
52 if (initializer
!= null) {
53 final PsiType type
= initializer
.getType();
54 if (type
!= null && "java.awt.Color".equals(type
.getCanonicalText())) {
55 if (initializer
instanceof PsiNewExpression
) {
56 final PsiExpressionList argumentList
= ((PsiNewExpression
) initializer
).getArgumentList();
57 if (argumentList
!= null) {
58 final PsiExpression
[] expressions
= argumentList
.getExpressions();
59 int[] values
= ArrayUtil
.newIntArray(expressions
.length
);
60 float[] values2
= new float[expressions
.length
];
64 final PsiConstantEvaluationHelper helper
= JavaPsiFacade
.getInstance(initializer
.getProject()).getConstantEvaluationHelper();
65 for (final PsiExpression each
: expressions
) {
66 final Object o
= helper
.computeConstantExpression(each
);
67 if (o
instanceof Integer
) {
68 values
[i
] = ((Integer
) o
).intValue();
69 values
[i
] = values
[i
] > 255 ?
255 : values
[i
] < 0 ?
0 : values
[i
];
71 } else if (o
instanceof Float
) {
72 values2
[j
] = ((Float
) o
).floatValue();
73 values2
[j
] = values2
[j
] > 1 ?
1 : values2
[j
] < 0 ?
0 : values2
[j
];
79 if (i
== expressions
.length
) {
80 switch (values
.length
) {
82 c
= new Color(values
[0]);
85 c
= new Color(values
[0], values
[1], values
[2]);
88 c
= new Color(values
[0], values
[1], values
[2], values
[3]);
93 } else if (j
== expressions
.length
) {
94 switch (values2
.length
) {
96 c
= new Color(values2
[0], values2
[1], values2
[2]);
99 c
= new Color(values2
[0], values2
[1], values2
[2], values2
[3]);
107 buffer
.append(generatePreviewHtml(c
));
110 } else if (initializer
instanceof PsiReferenceExpression
) {
111 final PsiReference reference
= initializer
.getReference();
112 if (reference
!= null) {
113 final PsiElement psiElement
= reference
.resolve();
114 if (psiElement
instanceof PsiField
) {
115 final PsiClass psiClass
= ((PsiField
) psiElement
).getContainingClass();
116 if (psiClass
!= null && "java.awt.Color".equals(psiClass
.getQualifiedName())) {
118 Field field
= Class
.forName("java.awt.Color").getField(((PsiField
)psiElement
).getName());
119 final Color c
= (Color
) field
.get(null);
120 buffer
.append(generatePreviewHtml(c
));
121 } catch (Exception e
) {