Skip to content

Commit

Permalink
Support hex int #30
Browse files Browse the repository at this point in the history
  • Loading branch information
junichi11 committed Jun 4, 2019
1 parent 8ed1cc6 commit b04e388
Show file tree
Hide file tree
Showing 13 changed files with 372 additions and 71 deletions.
Expand Up @@ -78,8 +78,8 @@ public List<ColorValue> getColorValues(Document document, String line, int lineN
List<ColorValue> colorValues = new ArrayList<>();
if (hasColorValue(line)) {
collectStandardColors(line, colorValues, lineNumber);
collectRGBColors(line, colorValues, lineNumber);
collectRGBAColors(line, colorValues, lineNumber);
collectIntRGBsColors(line, colorValues, lineNumber);
collectIntRGBAsColors(line, colorValues, lineNumber);
}
return colorValues;
}
Expand All @@ -93,12 +93,12 @@ private void collectStandardColors(String line, List<ColorValue> colorValues, in
colorValues.addAll(ColorsUtils.getJavaStandardColors(line, lineNumber));
}

private void collectRGBColors(String line, List<ColorValue> colorValues, int lineNumber) {
colorValues.addAll(ColorsUtils.getJavaIntRGBColors(line, lineNumber));
private void collectIntRGBsColors(String line, List<ColorValue> colorValues, int lineNumber) {
colorValues.addAll(ColorsUtils.getJavaIntRGBsColors(line, lineNumber));
}

private void collectRGBAColors(String line, List<ColorValue> colorValues, int lineNumber) {
colorValues.addAll(ColorsUtils.getJavaIntRGBAColors(line, lineNumber));
private void collectIntRGBAsColors(String line, List<ColorValue> colorValues, int lineNumber) {
colorValues.addAll(ColorsUtils.getJavaIntRGBAsColors(line, lineNumber));
}

@Override
Expand All @@ -120,14 +120,14 @@ public List<ColorCodeGeneratorItem> getColorCodeGeneratorItems(String mimeType)
}

private static enum JavaColorCodeGeneratorItem implements ColorCodeGeneratorItem {
INT_RGB(JavaColorType.JAVA_INT_RGB) {
INT_RGB(JavaColorType.JAVA_INT_R_G_B) {
@Override
public String getDisplayName() {
return "new Color(r, g, b)"; // NOI18N
}

},
INT_RGBA(JavaColorType.JAVA_INT_RGBA) {
INT_RGBA(JavaColorType.JAVA_INT_R_G_B_A) {
@Override
public String getDisplayName() {
return "new Color(r, g, b, a)"; // NOI18N
Expand Down
@@ -0,0 +1,40 @@
/*
* Copyright 2019 junichi11.
*
* 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.junichi11.netbeans.modules.color.codes.preview.impl.colors;

/**
*
* @author junichi11
*/
public enum IntType {
Decimal(10, "%d"), // NOI18N
Hex(16, "%02x"); // NOI18N
private final int radix;
private final String formatSpecifier;

private IntType(int radix, String formatSpecifier) {
this.radix = radix;
this.formatSpecifier = formatSpecifier;
}

public int getRadix() {
return radix;
}

public String getFormatSpecifier() {
return formatSpecifier;
}
}
Expand Up @@ -31,9 +31,15 @@ public final class JavaColorCodeFormatter implements ColorCodeFormatter {
private static final String DECODE_HEX_VALUE_FORMAT = "Color.decode(\"#%02x%02x%02x\")"; // NOI18N

private final JavaColorType type;
private final RGBAIntTypes rgbaIntTypes;

public JavaColorCodeFormatter(JavaColorType type) {
public JavaColorCodeFormatter(JavaColorType type, RGBAIntTypes rgbaIntTypes) {
this.type = type;
this.rgbaIntTypes = rgbaIntTypes;
}

public JavaColorCodeFormatter(JavaColorType type) {
this(type, RGBAIntTypes.ALL_DECIMAL);
}

@Override
Expand All @@ -50,16 +56,49 @@ public String format(Color color) {
}

private String asRGBIntColorValue(Color color) {
if (type == JavaColorType.JAVA_INT_RGBA || color.getAlpha() != 255) {
return String.format(RGBA_VALUE_FORMAT, color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha());
assert rgbaIntTypes != null;
if (type == JavaColorType.JAVA_INT_R_G_B_A || color.getAlpha() != 255) {
return String.format(buildColorRGBAsFormat(rgbaIntTypes, true), color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha());
}
return String.format(RGB_VALUE_FORMAT, color.getRed(), color.getGreen(), color.getBlue());
return String.format(buildColorRGBAsFormat(rgbaIntTypes, false), color.getRed(), color.getGreen(), color.getBlue());
}

private String asDecodeHexValue(Color color) {
return String.format(DECODE_HEX_VALUE_FORMAT, color.getRed(), color.getGreen(), color.getBlue());
}

private static String buildColorRGBAsFormat(RGBAIntTypes rgbaIntTypes, boolean hasAlpha) {
StringBuilder sb = new StringBuilder();
sb.append("Color("); // NOI18N
if (IntType.Hex == rgbaIntTypes.getRed()) {
sb.append("0x"); // NOI18N
}
sb.append(rgbaIntTypes.getRed().getFormatSpecifier());
sb.append(", "); // NOI18N
if (IntType.Hex == rgbaIntTypes.getGreen()) {
sb.append("0x"); // NOI18N
}
sb.append(rgbaIntTypes.getGreen().getFormatSpecifier());
sb.append(", "); // NOI18N
if (IntType.Hex == rgbaIntTypes.getBlue()) {
sb.append("0x"); // NOI18N
}
sb.append(rgbaIntTypes.getBlue().getFormatSpecifier());
if (hasAlpha) {
sb.append(", "); // NOI18N
IntType alpha = rgbaIntTypes.getAlpha();
if (alpha == null) {
alpha = rgbaIntTypes.getRed();
}
if (IntType.Hex == alpha) {
sb.append("0x"); // NOI18N
}
sb.append(alpha.getFormatSpecifier());
}
sb.append(")"); // NOI18N
return sb.toString();
}

@CheckForNull
private String getStandardJavaColor(Color selectedColor) {
for (JavaStandardColor value : JavaStandardColor.values()) {
Expand Down
Expand Up @@ -26,21 +26,23 @@
*
* @author junichi11
*/
public class JavaIntRGBAColorValue extends AbstractColorValue {
public class JavaIntRGBAsColorValue extends AbstractColorValue {

private final int r;
private final int g;
private final int b;
private final int a;
private final RGBAIntTypes rgbaIntTypes;
private static final String GTK_LOOK_AND_FEEL_NAME = "GTK look and feel"; // NOI18N
private static final String LOOK_AND_FEEL_NAME = UIManager.getLookAndFeel().getName();

public JavaIntRGBAColorValue(String value, OffsetRange offsetRange, int line, Color color) {
public JavaIntRGBAsColorValue(String value, OffsetRange offsetRange, int line, Color color, RGBAIntTypes rgbaIntTypes) {
super(value, offsetRange, line);
this.r = color.getRed();
this.g = color.getGreen();
this.b = color.getBlue();
this.a = color.getAlpha();
this.rgbaIntTypes = rgbaIntTypes;
}

@Override
Expand All @@ -49,7 +51,7 @@ public Color getColor() {
}

public JavaColorType getType() {
return JavaColorType.JAVA_INT_RGBA;
return JavaColorType.JAVA_INT_R_G_B_A;
}

@Override
Expand All @@ -63,4 +65,7 @@ public ColorCodeFormatter getFormatter() {
return new JavaColorCodeFormatter(getType());
}

public RGBAIntTypes getRGBAIntTypes() {
return rgbaIntTypes;
}
}
Expand Up @@ -25,17 +25,19 @@
*
* @author arsi
*/
public class JavaIntRGBColorValue extends AbstractColorValue {
public class JavaIntRGBsColorValue extends AbstractColorValue {

private final int r;
private final int g;
private final int b;
private final RGBAIntTypes rgbaIntTypes;

public JavaIntRGBColorValue(String value, OffsetRange offsetRange, int line, Color color) {
public JavaIntRGBsColorValue(String value, OffsetRange offsetRange, int line, Color color, RGBAIntTypes rgbaIntTypes) {
super(value, offsetRange, line);
this.r = color.getRed();
this.g = color.getGreen();
this.b = color.getBlue();
this.rgbaIntTypes = rgbaIntTypes;
}

@Override
Expand All @@ -44,7 +46,7 @@ public Color getColor() {
}

public JavaColorType getType() {
return JavaColorType.JAVA_INT_RGB;
return JavaColorType.JAVA_INT_R_G_B;
}

@Override
Expand All @@ -57,4 +59,7 @@ public ColorCodeFormatter getFormatter() {
return new JavaColorCodeFormatter(getType());
}

public RGBAIntTypes getRGBAIntTypes() {
return rgbaIntTypes;
}
}
@@ -0,0 +1,81 @@
/*
* Copyright 2019 junichi11.
*
* 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.junichi11.netbeans.modules.color.codes.preview.impl.colors;

import org.netbeans.api.annotations.common.CheckForNull;

/**
*
* @author junichi11
*/
public final class RGBAIntTypes {

private final IntType r;
private final IntType g;
private final IntType b;
private final IntType a;
private final IntType rgba;
private static final IntType DEFAULT_TYPE = IntType.Decimal;
public static final RGBAIntTypes ALL_DECIMAL = new RGBAIntTypes(IntType.Decimal, IntType.Decimal, IntType.Decimal, IntType.Decimal, IntType.Decimal);
public static final RGBAIntTypes ALL_HEX = new RGBAIntTypes(IntType.Hex, IntType.Hex, IntType.Hex, IntType.Hex, IntType.Hex);

private RGBAIntTypes(IntType r, IntType g, IntType b, IntType a, IntType rgba) {
this.r = r;
this.g = g;
this.b = b;
this.a = a;
this.rgba = rgba;
}

public RGBAIntTypes(IntType r, IntType g, IntType b, IntType a) {
this(r, g, b, a, null);
}

public RGBAIntTypes(IntType r, IntType g, IntType b) {
this(r, g, b, null, null);

}

public RGBAIntTypes(IntType rgba) {
this(null, null, null, null, rgba);
}

@CheckForNull
public IntType getRed() {
return r;
}

@CheckForNull
public IntType getGreen() {
return g;
}

@CheckForNull
public IntType getBlue() {
return b;
}

@CheckForNull
public IntType getAlpha() {
return a;
}

@CheckForNull
public IntType getRgba() {
return rgba;
}

}
Expand Up @@ -23,7 +23,8 @@
*/
public interface ColorType {

static final String INT_RGB_VALUE_FORMAT = "25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9]"; // NOI18N
static final String DECIMAL_INT_R_G_B_VALUE_REGEX = "25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9]"; // NOI18N
static final String HEX_INT_R_G_B_VALUE_REGEX = "[0-9a-fA-F]{1,2}"; // NOI18N

Pattern getPattern();
}

0 comments on commit b04e388

Please sign in to comment.