Solved Smooth transitions for leather armor colors

Discussion in 'Spigot Plugin Development' started by MineStein, Apr 17, 2018.


    This is my current code. It works but it is jumpy and does not have the smooth effect I'm looking for. How would I achieve a smooth transition between colors?
  2. After you set the players armor try doing player.updateInventory();
  3. The problem is not that it does not work, it does. I'm just looking for advice on how to smoothly transition between colors.
  4. Enchant the armor it may make it look nicer. Unless your problem is that it switches too fast? You could add a timer to it so it switches every 5seconds or whatever.
  5. What?
  6. After you do
    1. if (boots != null && boots.getType().equals(Material.LEATHER_BOOTS)) {
    2. p.getInventory().setBoots(getColorArmor(Material.LEATHER_BOOTS, c));
    3. }
    Try p.updateInventory();

    That will update the armor and should stop any issues.
  7. You don't seem to understand. There aren't any issues. I'm asking how to calculate the RGB value for smooth transition instead of a random one.
  8. md_5

    Administrator Developer

    Pretend its linear and just add the two rgb colours together, weighted by a gradually increasing value from 0.0 to 1.0
  9. And I'm confused lol
  10. md_5

    Administrator Developer

    You have your start color and end colour.
    Each time you want to transition between start and end create a new color with r = (1-step)startR+step*endR

    Repeat for G and B, and sweep step from 0.0 to 1.0
    • Agree Agree x 1
    • Informative Informative x 1
  11. What md_5 said. There's nothing in your current code that indicates that you want to be making a smooth transition since you're literally grabbing random colors, lol.
  12. That would be why I made the thread asking how I would achieve a different effect and stated multiple times that while what I have works, it is not what I am looking for.
  13. I'm still confused, I'm afraid. I'm looking to transition between all of the colors of the rainbow. What do you mean sweep step?
  14. make a variable, and every few ticks add to it 1. if the variable is 255 reset it to 0.
    and with this variable you will make your color.
    Code (Text):
    short smoothColor = 0; // every few tick add 1, when higher than 255, reset to 0;
    Color c = Color.fromRGB(smoothColor, smoothColor, smoothColor);
  15. If you want rainbow colors, one way would be to use sine waves:
    Code (Java):
    double twoPi = 2 * Math.PI;
    double time = currentTick * speed;
    double red = 127.5 + 127.5 * Math.sin(time);
    double green = 127.5 + 127.5 * Math.sin(time + twoPi * 1/3);
    double blue = 127.5 + 127.5 * Math.sin(time + twoPi * 2/3);
    We multiply by 127.5 and add 127.5, because if we just multiplied by 255, the value would range from -255 to 255, and anything below 0 is 0. Another way to represent it would be like this, whichever looks better to you:
    Code (Java):
    (1 + Math.sin(time))/2 * 255;
    The values could also be cached; you can nicely fit all red, green, blue (and alpha) values in a single int:
    Code (Java):
    int red = (int) ((1 + Math.sin(time))/2 * 255);
    int green = (int) ((1 + Math.sin(time + twoPi * 1/3))/2 * 255);
    int blue = (int) ((1 + Math.sin(time + twoPi * 2/3))/2 * 255);
    int combined = red << 16 | green << 8 | blue;
    And to unpack from the int:
    Code (Java):
    int red = combined >> 16 & 0xFF;
    int green = combined >> 8 & 0xFF;
    int blue = combined & 0xFF;
    Or just store an array of Colors, I guess that works too
    • Agree Agree x 1
    • Useful Useful x 1
  16. I found a good solution to this using HSB values in case anyone in the future is looking at this thread.

    Essentially, create a float field which is initialized as zero and increase it by about 1 / 30 every so often, resetting it back to zero when it is over 1. Using some bit shifting I don't understand entirely, get the color via java.awt.Color.HSBToRGB.
    • Useful Useful x 2
  17. RGB values are 0-255 so FF is masking just one byte for the 3 RGB values, 0xFFFFFF would then mask the lower 24 bits that are specified in the HSB to RGB method.

Share This Page