Class ColorUtils

java.lang.Object
net.kamkeyke.raccooncore.util.ColorUtils

public class ColorUtils extends Object
Utility methods for reading, writing and mixing item colors stored via NBT, following the same display.color convention vanilla uses for dyeable leather items.

Also resolves a DyeColor from colored block-item variants (wool, terracotta, concrete, ...) and mixes dyes together the same way vanilla does for leather armor.

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    getColor(net.minecraft.world.item.ItemStack stack, int fallbackColor)
    Reads the custom color stored in the item's display.color NBT tag.
    static Optional<net.minecraft.world.item.DyeColor>
    getWoolColor(net.minecraft.world.item.ItemStack stack)
     
    static boolean
    hasColor(net.minecraft.world.item.ItemStack stack)
     
    static int
    mixColors(int baseColor, Iterable<net.minecraft.world.item.ItemStack> dyes)
    Mixes an existing color (or none) with one or more dye stacks.
    static net.minecraft.world.item.Item
    resolveVariant(net.minecraft.world.item.DyeColor color, String modIdFromBlock, String suffix)
    Resolves a vanilla colored-block-item variant for a given DyeColor and suffix (e.g.
    static void
    setColor(net.minecraft.world.item.ItemStack stack, int rgb)
    Stores an RGB color (0xRRGGBB) in the stack's display.color NBT tag.
    static int
    toRgb(net.minecraft.world.item.DyeColor color)
    Converts a DyeColor into a packed 0xRRGGBB value.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ColorUtils

      public ColorUtils()
  • Method Details

    • resolveVariant

      public static net.minecraft.world.item.Item resolveVariant(net.minecraft.world.item.DyeColor color, String modIdFromBlock, String suffix)
      Resolves a vanilla colored-block-item variant for a given DyeColor and suffix (e.g. "_wool", "_terracotta", "_concrete").

      Use for suffixes other than wool, which already has a lookup table cached.

    • getWoolColor

      public static Optional<net.minecraft.world.item.DyeColor> getWoolColor(net.minecraft.world.item.ItemStack stack)
      Returns:
      the DyeColor matching the given stack, if it is a wool item.
    • toRgb

      public static int toRgb(net.minecraft.world.item.DyeColor color)
      Converts a DyeColor into a packed 0xRRGGBB value.
    • getColor

      public static int getColor(net.minecraft.world.item.ItemStack stack, int fallbackColor)
      Reads the custom color stored in the item's display.color NBT tag.
      Parameters:
      fallbackColor - value returned when the stack has no custom color set
    • hasColor

      public static boolean hasColor(net.minecraft.world.item.ItemStack stack)
      Returns:
      true if the stack has a custom color set via setColor(net.minecraft.world.item.ItemStack, int).
    • setColor

      public static void setColor(net.minecraft.world.item.ItemStack stack, int rgb)
      Stores an RGB color (0xRRGGBB) in the stack's display.color NBT tag.
    • mixColors

      public static int mixColors(int baseColor, Iterable<net.minecraft.world.item.ItemStack> dyes)
      Mixes an existing color (or none) with one or more dye stacks. Colors with a higher combined count dominate super-linearly (see MIX_POWER), and results close enough to a pure DyeColor snap to it exactly instead of leaving a faint residue.
      Parameters:
      baseColor - -1 if the item has no color yet, otherwise its current RGB color
      dyes - dye item stacks being applied (stack count is honored)