Search in sources :

Example 21 with Color

use of org.terasology.rendering.nui.Color in project Terasology by MovingBlocks.

the class PlayerSettingsScreen method findClosestIndex.

private float findClosestIndex(Color color) {
    int best = 0;
    float minDist = Float.MAX_VALUE;
    for (int i = 0; i < colors.size(); i++) {
        Color other = colors.get(i);
        float dr = other.rf() - color.rf();
        float dg = other.gf() - color.gf();
        float db = other.bf() - color.bf();
        // there are certainly smarter ways to measure color distance,
        // but Euclidean distance is good enough for the purpose
        float dist = dr * dr + dg * dg + db * db;
        if (dist < minDist) {
            minDist = dist;
            best = i;
        }
    }
    float max = colors.size() - 1;
    return best / max;
}
Also used : Color(org.terasology.rendering.nui.Color)

Example 22 with Color

use of org.terasology.rendering.nui.Color in project Terasology by MovingBlocks.

the class DocumentRenderer method renderParagraph.

public static int renderParagraph(Canvas canvas, ParagraphRenderable.HyperlinkRegister register, ParagraphRenderStyle baseRenderStyle, int xShift, int leftIndent, int rightIndent, ContainerRenderSpace containerRenderSpace, int containerWidth, int startY, ParagraphData paragraphData) {
    int yShift = startY;
    ParagraphRenderable paragraphContents = paragraphData.getParagraphContents();
    ParagraphRenderStyle paragraphRenderStyle = getParagraphRenderStyle(baseRenderStyle, paragraphData);
    ParagraphRenderStyle.ClearStyle clearStyle = paragraphRenderStyle.getClearStyle();
    if (clearStyle != ParagraphRenderStyle.ClearStyle.NONE) {
        yShift = Math.max(yShift, containerRenderSpace.getNextClearYPosition(clearStyle));
    }
    ParagraphRenderStyle.FloatStyle floatStyle = paragraphRenderStyle.getFloatStyle();
    if (floatStyle == ParagraphRenderStyle.FloatStyle.LEFT || floatStyle == ParagraphRenderStyle.FloatStyle.RIGHT) {
        int leftParagraphIndent = paragraphRenderStyle.getParagraphMarginLeft().getValue(containerWidth) + paragraphRenderStyle.getParagraphPaddingLeft().getValue(containerWidth);
        int rightParagraphIndent = paragraphRenderStyle.getParagraphMarginRight().getValue(containerWidth) + paragraphRenderStyle.getParagraphPaddingRight().getValue(containerWidth);
        int paragraphWidth = Math.max(paragraphRenderStyle.getParagraphMinimumWidth().getValue(containerWidth), paragraphContents.getContentsMinWidth(paragraphRenderStyle) + leftParagraphIndent + rightParagraphIndent);
        int height = paragraphRenderStyle.getParagraphMarginTop().getValue(containerWidth) + paragraphRenderStyle.getParagraphPaddingTop().getValue(containerWidth);
        int paragraphHeight = paragraphContents.getPreferredContentsHeight(paragraphRenderStyle, 0, new ContainerFlowContainerRenderSpace(paragraphWidth), leftParagraphIndent + rightParagraphIndent);
        height += paragraphHeight;
        height += paragraphRenderStyle.getParagraphPaddingBottom().getValue(containerWidth) + paragraphRenderStyle.getParagraphMarginBottom().getValue(containerWidth);
        Rect2i position;
        if (floatStyle == ParagraphRenderStyle.FloatStyle.LEFT) {
            position = containerRenderSpace.addLeftFloat(yShift, paragraphWidth, height);
        } else {
            position = containerRenderSpace.addRightFloat(yShift, paragraphWidth, height);
        }
        Rect2i paragraphBorderRegion = Rect2i.createFromMinAndMax(position.minX() + paragraphRenderStyle.getParagraphMarginLeft().getValue(containerWidth), position.minY() + paragraphRenderStyle.getParagraphMarginTop().getValue(containerWidth), position.maxX() - paragraphRenderStyle.getParagraphMarginRight().getValue(containerWidth) - 1, position.maxY() - paragraphRenderStyle.getParagraphMarginBottom().getValue(containerWidth) - 1);
        Color paragraphBackground = paragraphRenderStyle.getParagraphBackground();
        if (paragraphBackground != null) {
            canvas.drawFilledRectangle(paragraphBorderRegion, paragraphBackground);
        }
        Vector2i paragraphStart = new Vector2i(position.minX(), position.minY() + paragraphRenderStyle.getParagraphMarginTop().getValue(containerWidth) + paragraphRenderStyle.getParagraphPaddingTop().getValue(containerWidth));
        paragraphContents.renderContents(canvas, paragraphStart, new ContainerFlowContainerRenderSpace(paragraphWidth), leftParagraphIndent, rightParagraphIndent, paragraphRenderStyle, paragraphRenderStyle.getHorizontalAlignment(), register);
        yShift = position.minY();
    } else {
        yShift += paragraphRenderStyle.getParagraphMarginTop().getValue(containerWidth);
        int leftParagraphIndent = paragraphRenderStyle.getParagraphMarginLeft().getValue(containerWidth) + paragraphRenderStyle.getParagraphPaddingLeft().getValue(containerWidth);
        int rightParagraphIndent = paragraphRenderStyle.getParagraphMarginRight().getValue(containerWidth) + paragraphRenderStyle.getParagraphPaddingRight().getValue(containerWidth);
        int paragraphHeight = paragraphContents.getPreferredContentsHeight(paragraphRenderStyle, yShift + paragraphRenderStyle.getParagraphPaddingTop().getValue(containerWidth), containerRenderSpace, leftIndent + leftParagraphIndent + rightParagraphIndent + rightIndent);
        Color paragraphBackground = paragraphRenderStyle.getParagraphBackground();
        if (paragraphBackground != null) {
            int borderAdvance = 0;
            int borderHeight = paragraphHeight + paragraphRenderStyle.getParagraphPaddingTop().getValue(containerWidth) + paragraphRenderStyle.getParagraphPaddingBottom().getValue(containerWidth);
            while (borderAdvance < borderHeight) {
                int backgroundStart = yShift + borderAdvance;
                int availableBackgroundWidth = containerRenderSpace.getWidthForVerticalPosition(backgroundStart);
                int backgroundAdvance = containerRenderSpace.getAdvanceForVerticalPosition(backgroundStart);
                int maxSpace = containerRenderSpace.getNextWidthChange(backgroundStart);
                Rect2i backgroundRegion = Rect2i.createFromMinAndSize(xShift + paragraphRenderStyle.getParagraphMarginLeft().getValue(containerWidth) + backgroundAdvance, backgroundStart, availableBackgroundWidth - 1, Math.min(maxSpace, borderHeight - borderAdvance) - 1);
                canvas.drawFilledRectangle(backgroundRegion, paragraphBackground);
                borderAdvance += maxSpace - backgroundStart;
            }
        }
        yShift += paragraphRenderStyle.getParagraphPaddingTop().getValue(containerWidth);
        paragraphContents.renderContents(canvas, new Vector2i(xShift, yShift), containerRenderSpace, leftIndent + leftParagraphIndent, rightIndent + rightParagraphIndent, paragraphRenderStyle, paragraphRenderStyle.getHorizontalAlignment(), register);
        yShift += paragraphHeight;
        yShift += paragraphRenderStyle.getParagraphPaddingBottom().getValue(containerWidth);
        yShift += paragraphRenderStyle.getParagraphMarginBottom().getValue(containerWidth);
    }
    return yShift - startY;
}
Also used : Rect2i(org.terasology.math.geom.Rect2i) Color(org.terasology.rendering.nui.Color) Vector2i(org.terasology.math.geom.Vector2i) FallbackParagraphRenderStyle(org.terasology.rendering.nui.widgets.browser.ui.style.FallbackParagraphRenderStyle) ParagraphRenderStyle(org.terasology.rendering.nui.widgets.browser.ui.style.ParagraphRenderStyle)

Example 23 with Color

use of org.terasology.rendering.nui.Color in project Terasology by MovingBlocks.

the class DocumentRenderer method drawDocumentInRegion.

public static void drawDocumentInRegion(DocumentData documentData, Canvas canvas, Font defaultFont, Color defaultColor, Vector2i size, ParagraphRenderable.HyperlinkRegister register) {
    DefaultDocumentRenderStyle defaultDocumentRenderStyle = new DefaultDocumentRenderStyle(defaultFont, defaultColor);
    DocumentRenderStyle documentRenderStyle = getDocumentRenderStyle(defaultDocumentRenderStyle, documentData);
    int documentWidth = size.x;
    int documentMarginLeft = documentRenderStyle.getDocumentMarginLeft().getValue(documentWidth);
    int documentMarginRight = documentRenderStyle.getDocumentMarginRight().getValue(documentWidth);
    int documentMarginTop = documentRenderStyle.getDocumentMarginTop().getValue(documentWidth);
    Color backgroundColor = documentRenderStyle.getBackgroundColor();
    if (backgroundColor != null) {
        canvas.drawFilledRectangle(canvas.getRegion(), backgroundColor);
    }
    Collection<ParagraphData> paragraphs = documentData.getParagraphs();
    ContainerFlowContainerRenderSpace renderSpace = new ContainerFlowContainerRenderSpace(documentWidth);
    renderParagraphs(canvas, register, documentRenderStyle, documentMarginLeft, documentMarginTop, documentMarginLeft, documentMarginRight, paragraphs, renderSpace);
}
Also used : DocumentRenderStyle(org.terasology.rendering.nui.widgets.browser.ui.style.DocumentRenderStyle) DefaultDocumentRenderStyle(org.terasology.rendering.nui.widgets.browser.ui.style.DefaultDocumentRenderStyle) FallbackDocumentRenderStyle(org.terasology.rendering.nui.widgets.browser.ui.style.FallbackDocumentRenderStyle) Color(org.terasology.rendering.nui.Color) ParagraphData(org.terasology.rendering.nui.widgets.browser.data.ParagraphData) DefaultDocumentRenderStyle(org.terasology.rendering.nui.widgets.browser.ui.style.DefaultDocumentRenderStyle)

Example 24 with Color

use of org.terasology.rendering.nui.Color in project Terasology by MovingBlocks.

the class ServerConnectionHandler method receivedConnect.

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);
}
Also used : Color(org.terasology.rendering.nui.Color)

Example 25 with Color

use of org.terasology.rendering.nui.Color in project Terasology by MovingBlocks.

the class PlayerConfig method defaultPlayerColor.

/**
 * Randomly generates a default color for the player via a random int generator using FastRandom object.
 *
 * @return a Color object with the player's default color.
 */
private Color defaultPlayerColor() {
    Random rng = new FastRandom();
    List<Color> colors = CieCamColors.L65C65;
    return colors.get(rng.nextInt(colors.size()));
}
Also used : Random(org.terasology.utilities.random.Random) FastRandom(org.terasology.utilities.random.FastRandom) Color(org.terasology.rendering.nui.Color) FastRandom(org.terasology.utilities.random.FastRandom)

Aggregations

Color (org.terasology.rendering.nui.Color)30 Test (org.junit.Test)9 ColorModel (java.awt.image.ColorModel)3 DataBufferInt (java.awt.image.DataBufferInt)3 ResourceUrn (org.terasology.assets.ResourceUrn)3 Rect2i (org.terasology.math.geom.Rect2i)3 Vector2i (org.terasology.math.geom.Vector2i)3 Font (org.terasology.rendering.assets.font.Font)3 Map (java.util.Map)2 EntityRef (org.terasology.entitySystem.entity.EntityRef)2 PersistedData (org.terasology.persistence.typeHandling.PersistedData)2 ColorBlender (org.terasology.world.viewer.color.ColorBlender)2 Maps (com.google.common.collect.Maps)1 Sets (com.google.common.collect.Sets)1 Gson (com.google.gson.Gson)1 JsonArray (com.google.gson.JsonArray)1 Graphics2D (java.awt.Graphics2D)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 ByteBuffer (java.nio.ByteBuffer)1