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

31
LINES

< > BotCompany Repo | #1034860 // multiPointGradientPixel

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

Libraryless. Click here for Pure Java version (5823L/32K).

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

/**
 * Given some points with color attached, calculate the color for a new point
 * @param  p The new point position {x: number, y: number}
 * @param  points The array of given colored points [{x: nember, y: number, c: hexColor}]
 * @return hex color string -- The weighted color mix
 */
static Color multiPointGradientPixel(DoublePt p, LPair<DoublePt, Color> gradientPoints) {
  ret multiPointGradientPixel(p.x, p.y, gradientPoints);
}

static Color multiPointGradientPixel(double px, double py, LPair<DoublePt, Color> gradientPoints) {
  int n = l(gradientPoints);
  double[] colorRatios = new[n];
  fillArray(colorRatios, 1);
  
  for i to n:
    for j to n:
      if (i != j) {
        var p1 = gradientPoints.get(i).a;
        var p2 = gradientPoints.get(j).a;
        var d = getProjectionDistance2(p1.x, p1.y, p2.x, p2.y, px, py);
        colorRatios[i] *= clampZeroToOne(d);
      
        printVars ifdef multiPointGradientPixel_debug(+p, +i, +j, +p1, +p2, d := asList(d), colorRatio := colorRatios[i]);
      }
      
    ret blendMultipleColors(countIteratorAsList(l(gradientPoints),
     i -> pair(gradientPoints.get(i).b, colorRatios[i])));
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1034860
Snippet name: multiPointGradientPixel
Eternal ID of this version: #1034860/11
Text MD5: 4468e9dabfadd46fee2ac27ba858a074
Transpilation MD5: cd28f6ef20804e08989d384b3ce29882
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 20:28:29
Source code size: 1233 bytes / 31 lines
Pitched / IR pitched: No / No
Views / Downloads: 73 / 149
Version history: 10 change(s)
Referenced in: [show references]