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

61
LINES

< > BotCompany Repo | #1034870 // MultiPointGradient for export

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

Libraryless. Click here for Pure Java version (1538L/8K).

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

Author comment

Began life as a copy of #1034865

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034870
Snippet name: MultiPointGradient for export
Eternal ID of this version: #1034870/8
Text MD5: 4b8b892c297294494fc0797e22e910d8
Transpilation MD5: b9e3a08432737cd766a3c5055cb8a5f1
Author: stefan
Category: javax / imaging
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-12 22:55:37
Source code size: 1717 bytes / 61 lines
Pitched / IR pitched: No / No
Views / Downloads: 712 / 861
Version history: 7 change(s)
Referenced in: [show references]