2 * Copyright 2000-2016 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.codeHighlighting;
18 import com.intellij.codeInsight.daemon.impl.HighlightInfo;
19 import com.intellij.codeInsight.daemon.impl.HighlightInfoType;
20 import com.intellij.lang.annotation.HighlightSeverity;
21 import com.intellij.openapi.application.ApplicationManager;
22 import com.intellij.openapi.editor.DefaultLanguageHighlighterColors;
23 import com.intellij.openapi.editor.colors.EditorColorsManager;
24 import com.intellij.openapi.editor.colors.TextAttributesKey;
25 import com.intellij.openapi.editor.colors.TextAttributesScheme;
26 import com.intellij.openapi.editor.markup.TextAttributes;
27 import com.intellij.openapi.util.registry.Registry;
28 import com.intellij.openapi.util.text.StringUtil;
29 import com.intellij.psi.PsiElement;
30 import com.intellij.ui.ColorUtil;
31 import com.intellij.ui.JBColor;
32 import org.jetbrains.annotations.NotNull;
33 import org.jetbrains.annotations.Nullable;
36 import java.util.ArrayList;
37 import java.util.List;
38 import java.util.stream.Collectors;
40 import static com.intellij.util.ObjectUtils.notNull;
42 public class RainbowHighlighter {
43 private final static String UNIT_TEST_COLORS = "#000001,#000002,#000003,#000004"; // Do not modify!
44 private final static int COLOR_COUNT = 16;
46 @NotNull private final TextAttributesScheme myColorsScheme;
47 @NotNull private final List<Color> myRainbowColors;
49 public RainbowHighlighter(@Nullable TextAttributesScheme colorsScheme) {
50 myColorsScheme = colorsScheme != null ? colorsScheme : EditorColorsManager.getInstance().getGlobalScheme();
51 myRainbowColors = generateColorSequence(myColorsScheme);
54 public static final HighlightInfoType RAINBOW_ELEMENT =
55 new HighlightInfoType.HighlightInfoTypeImpl(HighlightSeverity.INFORMATION, DefaultLanguageHighlighterColors.CONSTANT);
57 public static boolean isRainbowEnabled() {
58 return Registry.is("editor.rainbow.identifiers", false);
62 public Color calculateForeground(int colorIndex) {
63 return myRainbowColors.get(Math.abs(colorIndex) % myRainbowColors.size());
66 public int getColorsCount() {
67 return myRainbowColors.size();
70 private static List<Color> generateColorSequence(@NotNull TextAttributesScheme colorsScheme) {
71 String colorDump = ApplicationManager.getApplication().isUnitTestMode()
73 : Registry.get("rainbow.highlighter.colors").asString();
75 final List<String> registryColors = StringUtil.split(colorDump, ",");
76 if (!registryColors.isEmpty()) {
77 return registryColors.stream().map((s -> ColorUtil.fromHex(s.trim()))).collect(Collectors.toList());
80 List<Color> colors = new ArrayList<Color>(COLOR_COUNT);
81 TextAttributes attributes = colorsScheme.getAttributes(DefaultLanguageHighlighterColors.CONSTANT);
82 Color foregroundColor = notNull(attributes != null ? attributes.getForegroundColor() : null, JBColor.gray);
83 float[] floats = Color.RGBtoHSB(foregroundColor.getRed(), foregroundColor.getGreen(), foregroundColor.getBlue(), null);
84 for (int i = 0; i < COLOR_COUNT; ++i) {
85 float factor = ((float)i) / COLOR_COUNT;
86 Color color = Color.getHSBColor(factor, .7f, floats[2] * 1.3f);
87 //noinspection UseJBColor
88 colors.add(new Color(color.getRed()/2, color.getGreen(), color.getBlue())); // to highlight errors we reduce Red color
93 public HighlightInfo getInfo(int colorIndex, @Nullable PsiElement id, @Nullable TextAttributesKey colorKey) {
97 if (colorKey == null) {
98 colorKey = DefaultLanguageHighlighterColors.LOCAL_VARIABLE;
101 .newHighlightInfo(RAINBOW_ELEMENT)
102 .textAttributes(TextAttributes
103 .fromFlyweight(myColorsScheme
104 .getAttributes(colorKey)
106 .withForeground(calculateForeground(colorIndex))))