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).

1  
// from https://codepen.io/tculda/pen/pogwpOw
2  
3  
sclass MultiPointGradient is MakesBufferedImage {
4  
  LPair<IDoublePt, Color> gradientPoints;
5  
  int w, h;
6  
  int[] pixels;
7  
  double[] gradientPointArray;
8  
  Color[] colors;
9  
  bool hasRun;
10  
  
11  
  *(LPair<IDoublePt, Color> *gradientPoints, int *w, int *h) {}
12  
  *(WidthAndHeight size, LPair<IDoublePt, Color> *gradientPoints) {
13  
    w = size.getWidth();
14  
    h = size.getHeight();
15  
  }
16  
  *(int *w, int *h, LPair<IDoublePt, Color> *gradientPoints) {}
17  
  
18  
  public int getWidth() { ret w; }
19  
  public int getHeight() { ret h; }
20  
  
21  
  public BufferedImage getBufferedImage() {
22  
    run();
23  
    ret intArrayToBufferedImageWithoutAlpha(pixels, w, h);
24  
  }
25  
  
26  
  void init {
27  
    if (pixels == null) pixels = new int[w*h];
28  
    if (gradientPointArray == null)
29  
      gradientPointArray = iDoublePtsToDoubleArray(pairsA(gradientPoints));
30  
    if (colors == null)
31  
      colors = toArray Color(pairsB(gradientPoints));
32  
  }
33  
34  
  run {
35  
    if (hasRun) ret;
36  
    set hasRun;
37  
    init();
38  
    
39  
    int n = l(colors);
40  
    double[] colorRatios = new[n];
41  
    int iPixel = 0;
42  
  
43  
    for y to h:
44  
      for x to w: {
45  
        fillArray(colorRatios, 1);
46  
        for i to n:
47  
          for j to n:
48  
          if (i != j) {
49  
            var d = getProjectionDistance2(
50  
              gradientPointArray[i*2], gradientPointArray[i*2+1],
51  
              gradientPointArray[j*2], gradientPointArray[j*2+1],
52  
              x, y);
53  
            colorRatios[i] *= clampZeroToOne(d);
54  
          }
55  
          
56  
        pixels[iPixel++] = blendMultipleColorsToInt(colors, colorRatios);
57  
      }
58  
  }
59  
}

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: 92 / 227
Version history: 13 change(s)
Referenced in: [show references]