| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ColorManager |
|
| 1.8571428571428572;1.857 |
| 1 | /* | |
| 2 | * Copyright 2004-2008 the Seasar Foundation and the Others. | |
| 3 | * | |
| 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 | |
| 7 | * | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | * | |
| 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, | |
| 13 | * either express or implied. See the License for the specific language | |
| 14 | * governing permissions and limitations under the License. | |
| 15 | */ | |
| 16 | package org.seasar.eclipse.common.util; | |
| 17 | ||
| 18 | import org.eclipse.jface.resource.ColorDescriptor; | |
| 19 | import org.eclipse.jface.resource.ColorRegistry; | |
| 20 | import org.eclipse.swt.graphics.Color; | |
| 21 | import org.eclipse.swt.graphics.RGB; | |
| 22 | ||
| 23 | /** | |
| 24 | * {@link Color} オブジェクトを管理するためのユーティリティクラスです。<br /> | |
| 25 | * | |
| 26 | * @author y-komori | |
| 27 | */ | |
| 28 | public class ColorManager { | |
| 29 | 4 | private static ColorRegistry registry = new ColorRegistry(); |
| 30 | ||
| 31 | private static final String SHARP = "#"; | |
| 32 | ||
| 33 | private static final String ZERO = "0"; | |
| 34 | ||
| 35 | 0 | private ColorManager() { |
| 36 | 0 | } |
| 37 | ||
| 38 | /** | |
| 39 | * カラーを登録します。<br /> | |
| 40 | * | |
| 41 | * @param symbolicName | |
| 42 | * カラー名称 | |
| 43 | * @param colorData | |
| 44 | * カラーデータ | |
| 45 | * @see ColorRegistry#put(String, RGB) | |
| 46 | */ | |
| 47 | public static void putColor(final String symbolicName, final RGB colorData) { | |
| 48 | 76 | registry.put(symbolicName, colorData); |
| 49 | 76 | } |
| 50 | ||
| 51 | /** | |
| 52 | * カラーを登録します。<br /> | |
| 53 | * カラー名称は <code>colorData</code> をもとに、<code>#RRGGBB</code> となります。<br /> | |
| 54 | * | |
| 55 | * @param colorData | |
| 56 | * カラーデータ | |
| 57 | * @see ColorRegistry#put(String, RGB) | |
| 58 | */ | |
| 59 | public static void putColor(final RGB colorData) { | |
| 60 | 0 | StringBuffer symbolicName = new StringBuffer(7); |
| 61 | 0 | symbolicName.append(SHARP).append(toFixedHexString(colorData.red)) |
| 62 | .append(toFixedHexString(colorData.green)).append( | |
| 63 | toFixedHexString(colorData.blue)); | |
| 64 | 0 | putColor(symbolicName.toString(), colorData); |
| 65 | 0 | } |
| 66 | ||
| 67 | private static String toFixedHexString(final int value) { | |
| 68 | 0 | return (value < 0x10) ? ZERO + Integer.toHexString(value) : Integer |
| 69 | .toHexString(value); | |
| 70 | } | |
| 71 | ||
| 72 | /** | |
| 73 | * 指定された名称の {@link Color} オブジェクトを返します。<br /> | |
| 74 | * | |
| 75 | * @param symbolicName | |
| 76 | * カラー名称 | |
| 77 | * @return {@link Color} オブジェクト | |
| 78 | * @see ColorRegistry#get(String) | |
| 79 | */ | |
| 80 | public static Color getColor(final String symbolicName) { | |
| 81 | 160 | Color color = registry.get(symbolicName); |
| 82 | 160 | if (color == null) { |
| 83 | 76 | String symbol = putColorByColorText(symbolicName); |
| 84 | 76 | if (symbol != null) { |
| 85 | 20 | color = registry.get(symbol); |
| 86 | } | |
| 87 | } | |
| 88 | 160 | return color; |
| 89 | } | |
| 90 | ||
| 91 | /** | |
| 92 | * 指定された名称の {@link ColorDescriptor} オブジェクトを返します。<br /> | |
| 93 | * | |
| 94 | * @param symbolicName | |
| 95 | * カラー名称 | |
| 96 | * @return {@link ColorDescriptor} オブジェクト | |
| 97 | * @see ColorRegistry#getColorDescriptor(String) | |
| 98 | */ | |
| 99 | public static ColorDescriptor getColorDescriptor(final String symbolicName) { | |
| 100 | 0 | ColorDescriptor color = registry.getColorDescriptor(symbolicName); |
| 101 | 0 | if (color == null) { |
| 102 | 0 | String symbol = putColorByColorText(symbolicName); |
| 103 | 0 | if (symbol != null) { |
| 104 | 0 | color = registry.getColorDescriptor(symbol); |
| 105 | } | |
| 106 | } | |
| 107 | 0 | return color; |
| 108 | } | |
| 109 | ||
| 110 | /** | |
| 111 | * <code>#RRGGBB</code> 形式のテキストを元にカラーを登録します。<br /> | |
| 112 | * 登録時のカラー名称は、<code>#RRGGBB</code> (ただし、<code>RRGGBB</code> | |
| 113 | * の部分は大文字に変換される)となります。 | |
| 114 | * | |
| 115 | * @param colorText | |
| 116 | * <code>#RRGGBB</code> 形式のテキスト | |
| 117 | * @return 登録したカラー名称 | |
| 118 | */ | |
| 119 | public static String putColorByColorText(final String colorText) { | |
| 120 | 76 | if (colorText.startsWith(SHARP) && colorText.length() == 7) { |
| 121 | 20 | int red = Integer.parseInt(colorText.substring(1, 3), 16); |
| 122 | 20 | int green = Integer.parseInt(colorText.substring(3, 5), 16); |
| 123 | 20 | int blue = Integer.parseInt(colorText.substring(5, 7), 16); |
| 124 | 20 | RGB rgb = new RGB(red, green, blue); |
| 125 | 20 | String newName = colorText.toUpperCase(); |
| 126 | 20 | putColor(newName, rgb); |
| 127 | ||
| 128 | 20 | return newName; |
| 129 | } | |
| 130 | 56 | return null; |
| 131 | } | |
| 132 | } |