update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / codeInsight / javadoc / ColorUtil.java
blob745693b94d717de484d175c83c4b7ac4a422df9d
1 /*
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;
22 import java.awt.*;
23 import java.lang.reflect.Field;
25 /**
26 * @author spleaner
28 public class ColorUtil {
29 private 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());
36 if (s.length() < 2) {
37 sb.append('0');
40 sb.append(s);
43 return sb.toString();
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;\">&nbsp;</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];
61 int i = 0;
62 int j = 0;
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];
70 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];
74 j++;
78 Color c = null;
79 if (i == expressions.length) {
80 switch (values.length) {
81 case 1:
82 c = new Color(values[0]);
83 break;
84 case 3:
85 c = new Color(values[0], values[1], values[2]);
86 break;
87 case 4:
88 c = new Color(values[0], values[1], values[2], values[3]);
89 break;
90 default:
91 break;
93 } else if (j == expressions.length) {
94 switch (values2.length) {
95 case 3:
96 c = new Color(values2[0], values2[1], values2[2]);
97 break;
98 case 4:
99 c = new Color(values2[0], values2[1], values2[2], values2[3]);
100 break;
101 default:
102 break;
106 if (c != null) {
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())) {
117 try {
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) {
122 // nothing