/** * @param ch the character to convert * @return the actual color */ public static Color toColor(char ch) { int rgb = ch - FIRST_COLOR; int r = (rgb >> 8) & 0xF; int g = (rgb >> 4) & 0xF; int b = (rgb >> 0) & 0xF; return new Color(r << 4, g << 4, b << 4); }
public CoreBiomeColors() { biomeColors.put(CoreBiome.DESERT, new Color(0xb0a087ff)); biomeColors.put(CoreBiome.MOUNTAINS, new Color(0x899a47ff)); biomeColors.put(CoreBiome.PLAINS, new Color(0x80b068ff)); biomeColors.put(CoreBiome.SNOW, new Color(0x99ffffff)); biomeColors.put(CoreBiome.FOREST, new Color(0x439765ff)); biomeColors.put(CoreBiome.OCEAN, new Color(0x44447aff)); biomeColors.put(CoreBiome.BEACH, new Color(0xd0c087ff)); }
public Color alterRed(int value) { Preconditions.checkArgument(value >= 0 && value <= MAX, "Color values must be in range 0-255"); return new Color(value << RED_OFFSET | (representation & RED_FILTER)); }
public Color alterBlue(int value) { Preconditions.checkArgument(value >= 0 && value <= MAX, "Color values must be in range 0-255"); return new Color(value << BLUE_OFFSET | (representation & BLUE_FILTER)); }
public Color alterAlpha(int value) { Preconditions.checkArgument(value >= 0 && value <= MAX, "Color values must be in range 0-255"); return new Color(value | (representation & ALPHA_FILTER)); }
public Color alterGreen(int value) { Preconditions.checkArgument(value >= 0 && value <= MAX, "Color values must be in range 0-255"); return new Color(value << GREEN_OFFSET | (representation & GREEN_FILTER)); }
private static Color parseColor(String colorStr) { if (colorStr == null || colorStr.length() != 7 || colorStr.charAt(0) != '#') { return null; } return new Color( Integer.parseInt(colorStr.substring(1, 3), 16), Integer.parseInt(colorStr.substring(3, 5), 16), Integer.parseInt(colorStr.substring(5, 7), 16), 255); } }
public Color inverse() { return new Color((~representation & ALPHA_FILTER) | a()); }
/** * Method to convert the color string hex representation back to a color. * Package-only access as it is for internal use in ColorTextureAssetResolver, * but should be here for maintenance with the color-to-color-string code. * * @param hexColorName RRGGBBAA in lower-case hex notation * @return color represented by hexColorName */ static/* package-only */Color getColorForColorName(String hexColorName) { if (hexColorName.length() != 8) { // TODO: we should probably log a warning in this case. return null; } String redString = hexColorName.substring(0, 2); String greenString = hexColorName.substring(2, 4); String blueString = hexColorName.substring(4, 6); String alphaString = hexColorName.substring(6); int red = Integer.parseInt(redString, 16); int green = Integer.parseInt(greenString, 16); int blue = Integer.parseInt(blueString, 16); int alpha = Integer.parseInt(alphaString, 16); return new Color(red, green, blue, alpha); }
@Before public void setUp() { color = new Color(1, 10, 60, 255); }
@Override public boolean startTag(String tag, Attributes attributes) { if (tag.equalsIgnoreCase("b")) { bold = true; return true; } else if (tag.equalsIgnoreCase("font")) { String name = HTMLUtils.findAttribute(attributes, "name"); if (name != null) { fontName = name; } String colorAttr = HTMLUtils.findAttribute(attributes, "color"); if (colorAttr != null && colorAttr.length() == 7 && colorAttr.charAt(0) == '#') { color = new Color(Integer.parseInt(colorAttr.substring(1), 16)); } return true; } else if (tag.equalsIgnoreCase("a")) { String href = HTMLUtils.findAttribute(attributes, "href"); if (href == null) { throw new HTMLParseException("Expected href attribute"); } hyperlink = href; } for (Map.Entry<String, FlowRenderableFactory> flowRenderableFactoryEntry : flowRenderableFactoryMap.entrySet()) { if (flowRenderableFactoryEntry.getKey().equalsIgnoreCase(tag)) { paragraphData.append(flowRenderableFactoryEntry.getValue().create(attributes, htmlFontResolver.getFont(fontName, bold), color, hyperlink)); return true; } } return false; }
@Test public void testSerialize() { SerializationContext serializationContext = Mockito.mock(SerializationContext.class); handler.serialize(new Color(0x010380FF), serializationContext); Mockito.verify(serializationContext).create(1, 3, 128, 255); }
@Test public void testColorTransformedToTextureUri() throws Exception { int red = 0x12; int green = 0x3; int blue = 0xc4; int alpha = 0xe; Color expectedColor = new Color(red, green, blue, alpha); int expectedRed = 18; int expectedGreen = 3; int expectedBlue = -60; int expectedAlpha = 14; testRepeatedColorInDataTexture(expectedColor, expectedRed, expectedGreen, expectedBlue, expectedAlpha); }
@Override public Color deserialize(PersistedData data, DeserializationContext context) { if (data.isArray()) { PersistedDataArray dataArray = data.getAsArray(); if (dataArray.isNumberArray() && dataArray.size() > 3) { TIntList vals = dataArray.getAsIntegerArray(); return new Color(vals.get(0), vals.get(1), vals.get(2), vals.get(3)); } } if (data.isString()) { String value = data.getAsString(); return new Color((int) Long.parseLong(value, 16)); } throw new DeserializationException("Expecting integer array or hex-string, but found: " + String.valueOf(data)); } }
private void receivedConnect(NetData.JoinMessage message) { logger.info("Received Start Join"); NetClient client = new NetClient(channelHandlerContext.getChannel(), networkSystem, identity); client.setPreferredName(message.getName()); client.setColor(new Color(message.getColor().getRgba())); client.setViewDistanceMode(ViewDistance.forIndex(message.getViewDistanceLevel())); channelHandlerContext.getPipeline().remove(this); serverHandler.connectionComplete(client); }
@Test public void testColorTextures() { Random r = new FastRandom(123456); for (int i = 0; i < 10; i++) { int rgba = r.nextInt(); Color red = new Color(rgba); ResourceUrn textureUriForColor = TextureUtil.getTextureUriForColor(red); String simpleString = textureUriForColor.toString(); Optional<Texture> tex = Assets.getTexture(simpleString); assertTrue(tex.isPresent()); ByteBuffer dataBuffer = tex.get().getData().getBuffers()[0]; int firstPixel = dataBuffer.asIntBuffer().get(0); Assert.assertEquals(rgba, firstPixel); } }
@Test public void testColorNameTransformedToColor() throws Exception { Color actualColor = TextureUtil.getColorForColorName("ff0000ff"); Color expectedColor = Color.RED; assertEquals(expectedColor, actualColor); actualColor = TextureUtil.getColorForColorName("1203c40e"); int red = 0x12; int green = 0x3; int blue = 0xc4; int alpha = 0xe; expectedColor = new Color(red, green, blue, alpha); assertEquals(expectedColor, actualColor); }
@Test public void testColorTransformedToAssetUriTransformedToColor() throws Exception { Color expectedColor = Color.RED; ResourceUrn assetUri = TextureUtil.getTextureUriForColor(expectedColor); Color actualColor = TextureUtil.getColorForColorName(assetUri.getFragmentName().toLowerCase()); assertEquals(expectedColor, actualColor); int red = 0x12; int green = 0x3; int blue = 0xc4; int alpha = 0xe; expectedColor = new Color(red, green, blue, alpha); assetUri = TextureUtil.getTextureUriForColor(expectedColor); actualColor = TextureUtil.getColorForColorName(assetUri.getFragmentName().toLowerCase()); assertEquals(expectedColor, actualColor); } }
@Test public void testColorTransformedToTextureUri() throws Exception { ResourceUrn assetUri = TextureUtil.getTextureUriForColor(Color.RED); assertEquals(TerasologyConstants.ENGINE_MODULE, assetUri.getModuleName()); assertEquals(new Name("color"), assetUri.getResourceName()); assertEquals(new Name("ff0000ff"), assetUri.getFragmentName()); int red = 0x12; int green = 0x3; int blue = 0xc4; int alpha = 0xe; assetUri = TextureUtil.getTextureUriForColor(new Color(red, green, blue, alpha)); assertEquals(TerasologyConstants.ENGINE_MODULE, assetUri.getModuleName()); assertEquals(new Name("color"), assetUri.getResourceName()); assertEquals(new Name("1203c40e"), assetUri.getFragmentName()); }