-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
hydra-gradientmap.js
154 lines (139 loc) · 4.3 KB
/
hydra-gradientmap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
{
const getHydra = function () {
const whereami = window.location?.href?.includes("hydra.ojack.xyz")
? "editor"
: window.atom?.packages
? "atom"
: "idk";
if (whereami === "editor") {
return window.hydraSynth;
}
if (whereami === "atom") {
return global.atom.packages.loadedPackages["atom-hydra"]
.mainModule.main.hydra;
}
let _h = [
window.hydraSynth,
window._hydra,
window.hydra,
window.h,
window.H,
window.hy
].find(h => h?.regl);
return _h;
};
window._hydra = getHydra();
window._hydraScope = _hydra.sandbox.makeGlobal ? window : _hydra.synth;
}
_hydra.synth.setFunction({
name: "lookupX",
type: "color",
inputs: [
{
name: "_tex",
type: "sampler2D",
default: o0,
},
{
name: "yOffset",
type: "float",
default: 0,
},
{
name: "blending",
type: "float",
default: 1,
},
],
glsl: `
float lum = _luminance(_c0.rgb);
vec2 pos = vec2(lum,yOffset);
vec4 color = texture2D(_tex,pos);
return mix(_c0,color,blending);
`,
});
_hydra.synth.setFunction({
name: "lookupY",
type: "color",
inputs: [
{
name: "_tex",
type: "sampler2D",
default: o0,
},
{
name: "xOffset",
type: "float",
default: 0,
},
{
name: "blending",
type: "float",
default: 1,
},
],
glsl: `
float lum = _luminance(_c0.rgb);
vec2 pos = vec2(xOffset,lum);
vec4 color = texture2D(_tex,pos);
return mix(_c0,color,blending);
`,
});
{
const hydraGradients = {};
hydraGradients.resolution = 512;
function createSource() {
const { constructor: HydraSource, regl, pb, width, height } = _hydra.s[0];
return new HydraSource({ regl, pb, width, height });
}
window.createLinearGradient = function(angle,...args) {
const cssColorFrom = (color) => {
const alpha = color.pop();
const cssColor = `rgba(${color
.map((cur) => Math.round(cur * 256))
.join(" ")}/ ${alpha})`;
return cssColor;
};
const normalizeArrayColor = (color) => {
if (Array.isArray(color)) {
return cssColorFrom(
Array.from({ length: 4 }).map((_, i) => color[i] ?? Number(i === 3))
);
} else {
return color;
}
};
const generateSteps = (q) =>
Array.from({ length: q }, (_, i) => i / (q - 1));
const generateBounds = (angle) => {
const radius = hydraGradients.resolution / 2;
const [s, c] = [Math.sin(angle), Math.cos(angle)];
const startX = radius - (c * radius);
const startY = radius - (s * radius);
const endX = radius + (c * radius);
const endY = radius + (s * radius);
return [startX, startY, endX, endY];
}
const bounds = generateBounds(angle);
const canvas = document.createElement("canvas");
canvas.width = hydraGradients.resolution;
canvas.height = hydraGradients.resolution;
const ctx = canvas.getContext("2d");
const hasCustomSteps = Number.isFinite(args[1]);
if (hasCustomSteps && args.length % 2 !== 0) args.push(1);
const filterEvery = hasCustomSteps ? 2 : 1;
let [colors, steps] = [
args.filter((_, i) => i % filterEvery ^ 1).map(normalizeArrayColor),
args.filter((_, i) => i % filterEvery),
];
steps = steps.length ? steps : generateSteps(colors.length);
const gradient = ctx.createLinearGradient(...bounds);
colors.forEach((color, i) => gradient.addColorStop(steps[i], color));
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, canvas.width, canvas.height);
const source = createSource();
source.init({ src: canvas }, { min: "linear", mag: "linear" });
return source;
}
window.createGradient = (...args) => window.createLinearGradient(0,...args);
}