MCPcopy Index your code
hub / github.com/plotly/plotly.js / cleanOne

Function cleanOne

src/components/color/index.js:143–183  ·  view source on GitHub ↗
(val)

Source from the content-addressed store, hash-verified

141};
142
143function cleanOne(val) {
144 if(isNumeric(val) || typeof val !== 'string') return val;
145
146 var valTrim = val.trim();
147 if(valTrim.slice(0, 3) !== 'rgb') return val;
148
149 var match = valTrim.match(/^rgba?\s*\(([^()]*)\)$/);
150 if(!match) return val;
151
152 var parts = match[1].trim().split(/\s*[\s,]\s*/);
153 var rgba = valTrim.charAt(3) === 'a' && parts.length === 4;
154 if(!rgba && parts.length !== 3) return val;
155
156 for(var i = 0; i < parts.length; i++) {
157 if(!parts[i].length) return val;
158 parts[i] = Number(parts[i]);
159
160 if(!(parts[i] >= 0)) {
161 // all parts must be non-negative numbers
162
163 return val;
164 }
165
166 if(i === 3) {
167 // alpha>1 gets clipped to 1
168
169 if(parts[i] > 1) parts[i] = 1;
170 } else if(parts[i] >= 1) {
171 // r, g, b must be < 1 (ie 1 itself is not allowed)
172
173 return val;
174 }
175 }
176
177 var rgbStr = Math.round(parts[0] * 255) + ', ' +
178 Math.round(parts[1] * 255) + ', ' +
179 Math.round(parts[2] * 255);
180
181 if(rgba) return 'rgba(' + rgbStr + ', ' + parts[3] + ')';
182 return 'rgb(' + rgbStr + ')';
183}

Callers 1

index.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…