MCPcopy Create free account
hub / github.com/Normal-Tangerine8609/Scriptable-Widgets / HTMLGradient

Function HTMLGradient

scripts/html-gradient.js:68–208  ·  view source on GitHub ↗
(gradient)

Source from the content-addressed store, hash-verified

66widget.presentSmall();
67
68async function HTMLGradient(gradient) {
69 // store colours for faster reuse. If you use this function multiple times, it may be better to move the following line to the top of your scriptable script
70 const colorCache = new Map();
71
72 // split into parts by commas not in quotes or brackets
73 let splitGradient = gradient
74 .split(/,(?![^(]*\))(?![^"']*["'](?:[^"']*["'][^"']*["'])*[^"']*$)/)
75 .map((e) => e.trim());
76
77 // get the direction from the first item of gradient
78 let gradientDirection;
79 const wordDirections = {
80 "to top": 0,
81 "to top right": 45,
82 "to right": 90,
83 "to bottom right": 135,
84 "to bottom": 180,
85 "to bottom left": 225,
86 "to left": 270,
87 "to top left": 315,
88 };
89 // check if it is a word direction, degrees direction or none are provided
90 const first = splitGradient[0].toLowerCase();
91 if (first in wordDirections) {
92 splitGradient.shift();
93 gradientDirection = wordDirections[first];
94 } else if (/\d+\s*deg/.test(first)) {
95 splitGradient.shift();
96 gradientDirection = Number(first.match(/(\d+)\s*deg/)[1]);
97 } else {
98 gradientDirection = 0;
99 }
100
101 // Get colours and locations
102 const colours = [];
103 const locations = [];
104 for (const part of splitGradient) {
105 // Get the location
106 const locationMatch = part.match(/\s+(\d+(?:\.\d+)?%?)$/);
107 let location = null;
108 let colorPart = part;
109
110 if (locationMatch) {
111 const rawLocation = locationMatch[1];
112 // Locations ending in % are percentages
113 if (rawLocation.endsWith("%")) {
114 location = Number(rawLocation.slice(0, -1)) / 100;
115 } else {
116 location = Number(rawLocation);
117 }
118 colorPart = part.slice(0, locationMatch.index).trim();
119 }
120 locations.push(location);
121
122 // Get the colour of the part
123 let color;
124 const [first, second] = colorPart.split("-");
125 if (second != null) {

Callers 1

html-gradient.jsFile · 0.85

Calls 1

colorFromValueFunction · 0.85

Tested by

no test coverage detected