Libraryless. Click here for Pure Java version (5823L/32K).
1 | // from https://codepen.io/tculda/pen/pogwpOw |
2 | |
3 | /** |
4 | * Given some points with color attached, calculate the color for a new point |
5 | * @param p The new point position {x: number, y: number} |
6 | * @param points The array of given colored points [{x: nember, y: number, c: hexColor}] |
7 | * @return hex color string -- The weighted color mix |
8 | */ |
9 | static Color multiPointGradientPixel(DoublePt p, LPair<DoublePt, Color> gradientPoints) { |
10 | ret multiPointGradientPixel(p.x, p.y, gradientPoints); |
11 | } |
12 | |
13 | static Color multiPointGradientPixel(double px, double py, LPair<DoublePt, Color> gradientPoints) { |
14 | int n = l(gradientPoints); |
15 | double[] colorRatios = new[n]; |
16 | fillArray(colorRatios, 1); |
17 | |
18 | for i to n: |
19 | for j to n: |
20 | if (i != j) { |
21 | var p1 = gradientPoints.get(i).a; |
22 | var p2 = gradientPoints.get(j).a; |
23 | var d = getProjectionDistance2(p1.x, p1.y, p2.x, p2.y, px, py); |
24 | colorRatios[i] *= clampZeroToOne(d); |
25 | |
26 | printVars ifdef multiPointGradientPixel_debug(+p, +i, +j, +p1, +p2, d := asList(d), colorRatio := colorRatios[i]); |
27 | } |
28 | |
29 | ret blendMultipleColors(countIteratorAsList(l(gradientPoints), |
30 | i -> pair(gradientPoints.get(i).b, colorRatios[i]))); |
31 | } |
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: | 134 / 230 |
Version history: | 10 change(s) |
Referenced in: | [show references] |