EverythingColor.java 文件源码

java
阅读 17 收藏 0 点赞 0 评论 0

项目:EverythingBlocks 作者:
/** Find the average color of an item */
public static int getAverageColorUnbounded(ItemStack stack) {

    ItemColors cols = Minecraft.getMinecraft().getItemColors();

    // null -> return white
    if(stack == null) return DEFAULT_COLOR;
    ItemStack nStack = stack.copy();
    nStack.stackSize = 1; // constant size (for counting / caching)
    Item item = nStack.getItem();

    // this stack overrides all color calculations
    if(item instanceof IOverrideEBColor) {
        return ((IOverrideEBColor)item).getEverythingBlockColor(nStack);
    }

    // non-overridden calculations cached for performance
    if(itemColorCache.containsKey(item.toString())) {
        return itemColorCache.get(item.toString());
    } else if(stackColorCache.containsKey(nStack.toString())) {
        return stackColorCache.get(nStack.toString());
    }

    // the color of potion stacks is their liquid color
    if(item instanceof ItemPotion) {
        return addStackToColorCache(nStack, cols.getColorFromItemstack(stack, 0));
    }

    // the color of spawn egg blocks is the average of their layers
    if(item instanceof ItemMonsterPlacer) {
        IntList colList = (IntList) new IntList().join(cols.getColorFromItemstack(stack, 0)).join(cols.getColorFromItemstack(stack, 1));
        return addStackToColorCache(nStack, ColorHelper.averageColors(colList));
    }

    // dyeable armor returns a slightly darkened dye color
    if(item instanceof ItemArmor && ((ItemArmor)item).getColor(stack) > -1) {
        return addStackToColorCache(nStack, ColorHelper.averageColors(((ItemArmor)item).getColor(stack), 0, 0.75));
    }

    // sensitive code
       try {
        // get the texture from the ItemStack (IN GAME, not in init methods or events!)
        TextureAtlasSprite sprite = Minecraft.getMinecraft().getRenderItem().getItemModelMesher().getItemModel(nStack).getParticleTexture();
        if(sprite == null) return addStackToColorCache(nStack, cols.getColorFromItemstack(stack, 0)); // default to item stack color
        int w = sprite.getIconWidth();
        int h = sprite.getIconHeight();

        // now look at the pixels
        IntList colors = new IntList();
        int[] aint = sprite.getFrameTextureData(0)[0];
        for(int x = 0; x < w; x++) {
            for(int y = 0; y < h; y++) {
                int c = aint[x + y * h];
                if(c == 0) continue; // skip alpha pixels
                colors.add(c);
            }
        }

        // this is the base color
        int baseColor = ColorHelper.averageColors(colors);

        // now get the defined item stack color
        int stackColor = cols.getColorFromItemstack(stack, 0);

        // get the subtractive mix, add it to the cache, and return!
        return addStackToColorCache(nStack, ColorHelper.subtractMixColors(baseColor, stackColor));
    } catch (Exception e) {
        Log.logger.warn("Exception caught getting color from ItemStack " + nStack.toString());
        return addStackToColorCache(nStack, DEFAULT_COLOR);
    }
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号