|
| 1 | +package graphql.scalars.color.hex; |
| 2 | + |
| 3 | +import graphql.language.StringValue; |
| 4 | +import graphql.language.Value; |
| 5 | +import graphql.schema.*; |
| 6 | + |
| 7 | +import java.awt.*; |
| 8 | +import java.util.function.Function; |
| 9 | +import java.util.regex.Pattern; |
| 10 | + |
| 11 | +import static graphql.scalars.util.Kit.typeName; |
| 12 | +/** |
| 13 | + * Access this via {@link graphql.scalars.ExtendedScalars#HexColorCode} |
| 14 | + * See the <a href="https://en.wikipedia.org/wiki/Web_colors">Web colors</a> for more details. |
| 15 | + * @implNote Supports the following formats: #RGB, #RGBA, #RRGGBB, #RRGGBBAA. Need to be prefixed with '#' |
| 16 | + */ |
| 17 | +public class HexColorCodeScalar { |
| 18 | + |
| 19 | + public static final GraphQLScalarType INSTANCE; |
| 20 | + |
| 21 | + |
| 22 | + static { |
| 23 | + Coercing<Color, String> coercing = new Coercing<Color, String>() { |
| 24 | + |
| 25 | + private final Pattern HEX_PATTERN = Pattern.compile("^(#([A-Fa-f0-9]{3,4}){1,2})$"); |
| 26 | + |
| 27 | + @Override |
| 28 | + public String serialize(Object input) throws CoercingSerializeException { |
| 29 | + Color color = parseColor(input, CoercingSerializeException::new); |
| 30 | + boolean hasAlpha = color.getAlpha() != 255; |
| 31 | + if (hasAlpha){ |
| 32 | + return String.format("#%02x%02x%02x%02x", color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha()); |
| 33 | + } else { |
| 34 | + return String.format("#%02x%02x%02x", color.getRed(), color.getGreen(), color.getBlue()); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public Color parseValue(Object input) throws CoercingParseValueException { |
| 40 | + return parseColor(input, CoercingParseValueException::new); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public Color parseLiteral(Object input) throws CoercingParseLiteralException { |
| 45 | + if (!(input instanceof StringValue)) { |
| 46 | + throw new CoercingParseLiteralException("Expected type 'StringValue' but was '" + typeName(input) + "'."); |
| 47 | + } |
| 48 | + String stringValue = ((StringValue) input).getValue(); |
| 49 | + return parseColor(stringValue, CoercingParseLiteralException::new); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public Value<?> valueToLiteral(Object input) { |
| 54 | + String s = serialize(input); |
| 55 | + return StringValue.newStringValue(s).build(); |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + private Color parseColor(Object input, Function<String, RuntimeException> exceptionMaker) { |
| 60 | + final Color result; |
| 61 | + if (input instanceof Color) { |
| 62 | + result = (Color) input; |
| 63 | + } else if (input instanceof String) { |
| 64 | + try { |
| 65 | + String hex = ((String) input); |
| 66 | + |
| 67 | + //validation |
| 68 | + //regex |
| 69 | + if (!HEX_PATTERN.matcher(hex).matches()) { |
| 70 | + throw new IllegalArgumentException("Invalid hex color code value : '" + input + "'."); |
| 71 | + } |
| 72 | + |
| 73 | + int i = Integer.decode(hex); |
| 74 | + int inputLength = hex.length(); |
| 75 | + |
| 76 | + if (inputLength == 4) { |
| 77 | + // #RGB |
| 78 | + result = new Color( |
| 79 | + (i >> 8 & 0xF) * 0x11, |
| 80 | + (i >> 4 & 0xF) * 0x11, |
| 81 | + (i & 0xF) * 0x11 |
| 82 | + ); |
| 83 | + } else if (inputLength == 5) { |
| 84 | + // #RGBA |
| 85 | + result = new Color( |
| 86 | + (i >> 12 & 0xF) * 0x11, |
| 87 | + (i >> 8 & 0xF) * 0x11, |
| 88 | + (i >> 4 & 0xF) * 0x11, |
| 89 | + (i & 0xF) * 0x11 |
| 90 | + ); |
| 91 | + } else if (inputLength == 7) { |
| 92 | + // #RRGGBB |
| 93 | + result = new Color(i); |
| 94 | + } else { |
| 95 | + // #RRGGBBAA |
| 96 | + result = new Color( |
| 97 | + (i >> 24 & 0xFF), |
| 98 | + (i >> 16 & 0xFF), |
| 99 | + (i >> 8 & 0xFF), |
| 100 | + (i & 0xFF) |
| 101 | + ); |
| 102 | + } |
| 103 | + } catch (NullPointerException | IllegalArgumentException ex) { |
| 104 | + throw exceptionMaker.apply("Invalid hex color code value : '" + input + "'. because of : '" + ex.getMessage() + "'"); |
| 105 | + } |
| 106 | + } else { |
| 107 | + throw exceptionMaker.apply("Expected a 'String' or 'Color' but was '" + typeName(input) + "'."); |
| 108 | + } |
| 109 | + return result; |
| 110 | + } |
| 111 | + |
| 112 | + }; |
| 113 | + |
| 114 | + INSTANCE = GraphQLScalarType.newScalar() |
| 115 | + .name("HexColorCode") |
| 116 | + .description("A field whose value is a hexadecimal color code: https://en.wikipedia.org/wiki/Web_colors.") |
| 117 | + .coercing(coercing).build(); |
| 118 | + } |
| 119 | + |
| 120 | +} |
0 commit comments