Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

59
LINES

< > BotCompany Repo | #1034865 // MultiPointGradient

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (6844L/38K).

// from https://codepen.io/tculda/pen/pogwpOw

sclass MultiPointGradient is MakesBufferedImage {
  LPair<IDoublePt, Color> gradientPoints;
  int w, h;
  int[] pixels;
  double[] gradientPointArray;
  Color[] colors;
  bool hasRun;
  
  *(LPair<IDoublePt, Color> *gradientPoints, int *w, int *h) {}
  *(WidthAndHeight size, LPair<IDoublePt, Color> *gradientPoints) {
    w = size.getWidth();
    h = size.getHeight();
  }
  *(int *w, int *h, LPair<IDoublePt, Color> *gradientPoints) {}
  
  public int getWidth() { ret w; }
  public int getHeight() { ret h; }
  
  public BufferedImage getBufferedImage() {
    run();
    ret intArrayToBufferedImageWithoutAlpha(pixels, w, h);
  }
  
  void init {
    if (pixels == null) pixels = new int[w*h];
    if (gradientPointArray == null)
      gradientPointArray = iDoublePtsToDoubleArray(pairsA(gradientPoints));
    if (colors == null)
      colors = toArray Color(pairsB(gradientPoints));
  }

  run {
    if (hasRun) ret;
    set hasRun;
    init();
    
    int n = l(colors);
    double[] colorRatios = new[n];
    int iPixel = 0;
  
    for y to h:
      for x to w: {
        fillArray(colorRatios, 1);
        for i to n:
          for j to n:
          if (i != j) {
            var d = getProjectionDistance2(
              gradientPointArray[i*2], gradientPointArray[i*2+1],
              gradientPointArray[j*2], gradientPointArray[j*2+1],
              x, y);
            colorRatios[i] *= clampZeroToOne(d);
          }
          
        pixels[iPixel++] = blendMultipleColorsToInt(colors, colorRatios);
      }
  }
}

Author comment

Began life as a copy of #1034860

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034865
Snippet name: MultiPointGradient
Eternal ID of this version: #1034865/14
Text MD5: d88153d9b43b0127162b11f6ad9b2788
Transpilation MD5: 49393b20f75618b1547eb2560de991e2
Author: stefan
Category: javax / imaging
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-13 01:05:51
Source code size: 1633 bytes / 59 lines
Pitched / IR pitched: No / No
Views / Downloads: 143 / 301
Version history: 13 change(s)
Referenced in: [show references]