/*
 * Copyright 2014 MovingBlocks
 *
 * 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 org.terasology.rendering.assets.texture;

import java.awt.Color;
import java.nio.ByteBuffer;

import org.terasology.rendering.assets.texture.Texture.FilterMode;
import org.terasology.rendering.assets.texture.Texture.WrapMode;

/**
 * Creates TextureData objects based on specific criteria
 * 
 * @author mkienenb
 */
public class TextureDataFactory {

    // Lwjgl 2.x currently requires textures to be powers of 16, although this should change in 3.0.
    private static final int TEXTURE_WIDTH = 16;
    private static final int TEXTURE_HEIGHT = 16;

    static public byte convertIntToByte(int value) {
        return value < 128 ? (byte) value : (byte) (value - 256);
    }

    /**
     * Create TextureData for a Texture all of a single color.
     * @param color to use for creating TextureData
     * @return TextureData created using specified color
     */
    public static TextureData newInstance(Color color) {

        // TODO: try this with UnsignedBytes.checkedCast() and see if it returns the same results.

        byte red = convertIntToByte(color.getRed());
        byte green = convertIntToByte(color.getGreen());
        byte blue = convertIntToByte(color.getBlue());
        byte alpha = convertIntToByte(color.getAlpha());

        ByteBuffer data = ByteBuffer.allocateDirect(4 * TEXTURE_WIDTH * TEXTURE_HEIGHT);
        for (int width = 0; width < TEXTURE_WIDTH; width++) {
            for (int height = 0; height < TEXTURE_HEIGHT; height++) {
                data.put(red).put(green).put(blue).put(alpha);
            }
        }

        // The buffer must be reset back to the initial position before passing it onward.
        data.rewind();

        return new TextureData(TEXTURE_WIDTH, TEXTURE_HEIGHT, new ByteBuffer[]{data}, WrapMode.Repeat, FilterMode.Nearest);
    }
}

@@ -826,16 +826,68 @@
 pMode;%0A%0A
+import com.google.common.primitives.UnsignedBytes;%0A%0A
 /**%0A * C
@@ -1276,56 +1276,39 @@
 urn 
-value %3C 128 ? (byte) value : (byte) (value - 256
+UnsignedBytes.checkedCast(value
 );%0A 
@@ -1571,30 +1571,18 @@
     
-// TODO: try this with
+byte red =
  Uns
@@ -1608,87 +1608,8 @@
 ast(
-) and see if it returns the same results.%0A%0A        byte red = convertIntToByte(
 colo
@@ -1642,32 +1642,41 @@
 green = 
-convertIntToByte
+UnsignedBytes.checkedCast
 (color.g
@@ -1707,32 +1707,41 @@
  blue = 
-convertIntToByte
+UnsignedBytes.checkedCast
 (color.g
@@ -1772,32 +1772,41 @@
 alpha = 
-convertIntToByte
+UnsignedBytes.checkedCast
 (color.g

Replace custom int-to-byte conversion with guava method.