Guest User

Pixmap Utility

a guest
Oct 8th, 2021
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.56 KB | None | 0 0
  1. package game.library.util;
  2.  
  3. import com.badlogic.gdx.graphics.Color;
  4. import com.badlogic.gdx.graphics.Pixmap;
  5. import com.badlogic.gdx.graphics.Pixmap.Blending;
  6. import com.badlogic.gdx.graphics.Pixmap.Format;
  7. import com.badlogic.gdx.math.Vector2;
  8.  
  9. public class PixmapUtil {
  10.  
  11.     public static Pixmap changeAlpha(Pixmap pixmap, float alpha) {
  12.         Pixmap newPixmap = new Pixmap(pixmap.getWidth(), pixmap.getHeight(), Format.RGBA8888);
  13.  
  14.         newPixmap.setBlending(Blending.SourceOver);
  15.         for (int x = 0; x < pixmap.getWidth(); x++) {
  16.             for (int y = 0; y < pixmap.getHeight(); y++) {
  17.                 int pixelValue = pixmap.getPixel(x, y);
  18.                 Color color = new Color(pixelValue);
  19.                 color.a = alpha;
  20.                 newPixmap.setColor(color);
  21.                 if ((pixelValue >> 24) != 0x00) {
  22.                     newPixmap.drawPixel(x, y);
  23.                 }
  24.             }
  25.         }
  26.         return newPixmap;
  27.     }
  28.  
  29.     public static Pixmap createOverlay(Pixmap pixmap, Color color) {
  30.         Pixmap newPixmap = new Pixmap(pixmap.getWidth(), pixmap.getHeight(), Format.RGBA8888);
  31.  
  32.         newPixmap.setBlending(Blending.SourceOver);
  33.         for (int x = 0; x < pixmap.getWidth(); x++) {
  34.             for (int y = 0; y < pixmap.getHeight(); y++) {
  35.                 int pixelValue = pixmap.getPixel(x, y);
  36.                 newPixmap.setColor(color);
  37.                 if ((pixelValue >> 24) != 0x00) {
  38.                     newPixmap.drawPixel(x, y);
  39.                 }
  40.             }
  41.         }
  42.         return newPixmap;
  43.     }
  44.  
  45.     public static Pixmap outline(Pixmap pixmap) {
  46.         Pixmap newPixmap = new Pixmap(pixmap.getWidth(), pixmap.getHeight(), Format.RGBA8888);
  47.  
  48.         for (int x = 0; x <= pixmap.getWidth(); x++) {
  49.             for (int y = 0; y <= pixmap.getHeight(); y++) {
  50.                 int pixelValue = pixmap.getPixel(x, y);
  51.                 int beforePixelValue = pixmap.getPixel(x - 1, y);
  52.                 int afterPixelValue = pixmap.getPixel(x + 1, y);
  53.                 if (((beforePixelValue >> 24) == 0x00 && (pixelValue >> 24) != 0x00) || (((afterPixelValue >> 24) == 0x00 && (pixelValue >> 24) != 0x00))) {
  54.                     newPixmap.setColor(Color.RED);
  55.                     newPixmap.drawPixel(x, y);
  56.                 }
  57.             }
  58.         }
  59.         return newPixmap;
  60.     }
  61.  
  62.     public static Pixmap createShadow(Pixmap pixmap) {
  63.         Pixmap newPixmap = new Pixmap(pixmap.getWidth(), pixmap.getHeight(), Format.RGBA8888);
  64.  
  65.         for (int x = 0; x < pixmap.getWidth(); x++) {
  66.             for (int y = 0; y < 25; y++) {
  67.                 Color color = Color.BLACK;
  68.                 int pixelValue = pixmap.getPixel(x, pixmap.getHeight() - y);
  69.                 if ((pixelValue >> 24) != 0x00) {
  70.                     color.a = 0.5f;
  71.                     newPixmap.setColor(color);
  72.                     newPixmap.drawPixel(x, pixmap.getHeight() - y);
  73.                 }
  74.             }
  75.         }
  76.         return newPixmap;
  77.     }
  78.  
  79.     public static Pixmap createLight(Color color, int radius) {
  80.         Pixmap pixmap = new Pixmap(radius * 2, radius * 2, Format.RGBA8888);
  81.         Vector2 tile = new Vector2(0, 0);
  82.         Vector2 center = new Vector2(radius, radius);
  83.  
  84.         for (int x = 0; x <= radius * 2; x++) {
  85.             for (int y = 0; y <= radius * 2; y++) {
  86.                 tile.set(x, y);
  87.                 float dx = center.x - tile.x, dy = center.y - tile.y;
  88.                 float distance = (float) Math.sqrt(dx * dx + dy * dy);
  89.                 if (distance <= radius) {
  90.                     color.a = 1 - center.dst(tile) / (float) radius;
  91.                     pixmap.setColor(color);
  92.                     pixmap.drawPixel(x, y);
  93.                 }
  94.             }
  95.         }
  96.         return pixmap;
  97.     }
  98.  
  99.     public static Pixmap createLight(Color color, int width, int height) {
  100.         Pixmap pixmap = new Pixmap(width, height, Format.RGBA8888);
  101.         Vector2 tile = new Vector2(0, 0);
  102.         Vector2 center = new Vector2(width / 2, height / 2);
  103.  
  104.         for (int x = 0; x <= width; x++) {
  105.             for (int y = 0; y <= height; y++) {
  106.                 tile.set(x, y);
  107.                 color.a = 1 - center.dst(tile) / Math.max(width, height);
  108.                 pixmap.setColor(color);
  109.                 pixmap.drawPixel(x, y);
  110.             }
  111.         }
  112.         return pixmap;
  113.     }
  114.  
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment