Skip to content

Commit 964eb84

Browse files
zackarydevmojoaxel
authored andcommitted
feat: better validation; allow hue wrap-around
1 parent d02e8d5 commit 964eb84

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

docs/graph3d/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,13 +395,13 @@ <h2 id="Configuration_Options">Configuration Options</h2>
395395
<td class="indent">surfaceColors.hue.start</td>
396396
<td>number</td>
397397
<td>None</td>
398-
<td>The hue value of the maximum z value of the surface. Hue is in degrees (0 to 360).</td>
398+
<td>The hue value of the maximum z value of the surface. Hue is in degrees with a period of 360.</td>
399399
</tr>
400400
<tr parent="surfaceColors" class="hidden">
401401
<td class="indent">surfaceColors.hue.end</td>
402402
<td>number</td>
403403
<td>None</td>
404-
<td>The hue value of the minimum z value of the surface. Hue is in degrees (0 to 360).</td>
404+
<td>The hue value of the minimum z value of the surface. Hue is in degrees with a period of 360.</td>
405405
</tr>
406406
<tr parent="surfaceColors" class="hidden">
407407
<td class="indent">surfaceColors.hue.saturation</td>

examples/graph3d/16_styling_surface.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@
7171
if (style === "hue") {
7272
options.surfaceColors = {
7373
hue: {
74-
start: 40,
75-
end: 180,
74+
start: -360,
75+
end: 360,
7676
saturation: 50,
7777
brightness: 100,
7878
colorStops: 8 // How many colour gradients do we want
@@ -125,8 +125,8 @@
125125
<code>
126126
<pre>surfaceColors: {
127127
hue: {
128-
start: 40,
129-
end: 180,
128+
start: -360,
129+
end: 360,
130130
saturation: 50,
131131
brightness: 100,
132132
colorStops: 8,

lib/graph3d/Settings.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,11 +488,27 @@ function setSurfaceColor(surfaceColors, dst) {
488488
let rgbColors = [];
489489

490490
if(Array.isArray(surfaceColors)) {
491-
rgbColors = surfaceColors.map(util.hexToRGB);
491+
// https://stackoverflow.com/questions/8027423/how-to-check-if-a-string-is-a-valid-hex-color-representation/8027444
492+
const hexTestRegex = /^#([0-9A-F]{3}){1,2}$/i;
493+
rgbColors = surfaceColors.map(function(colorCode){
494+
if(!hexTestRegex.test(colorCode)) {
495+
throw new Error('Invalid hex color code supplied to surfaceColors.');
496+
}
497+
return util.hexToRGB(colorCode);
498+
});
492499
} else if (typeof surfaceColors === 'object') {
493500
if(surfaceColors.hue === undefined) {
494501
throw new Error('Unsupported type of surfaceColors');
495502
}
503+
if(surfaceColors.hue.saturation < 0 || surfaceColors.hue.saturation > 100) {
504+
throw new Error('Surface colors saturation is out of bounds. Expected range is 0-100.');
505+
}
506+
if(surfaceColors.hue.brightness < 0 || surfaceColors.hue.brightness > 100) {
507+
throw new Error('Surface colors brightness is out of bounds. Expected range is 0-100.');
508+
}
509+
if(surfaceColors.hue.colorStops < 2) {
510+
throw new Error('Surface colors colorStops is out of bounds. Expected 2 or above.');
511+
}
496512

497513
const startHue = Math.min(surfaceColors.hue.start, surfaceColors.hue.end);
498514
const endHue = Math.max(surfaceColors.hue.start, surfaceColors.hue.end);
@@ -503,9 +519,13 @@ function setSurfaceColor(surfaceColors, dst) {
503519

504520
let currentHue = startHue;
505521
while (currentHue < endHue) {
522+
let calculatedHue = currentHue % 360;
523+
if(calculatedHue < 0) {
524+
calculatedHue = 360 + calculatedHue;
525+
}
506526
rgbColors.push(
507527
util.HSVToRGB(
508-
(currentHue % 360)/360,
528+
calculatedHue/360,
509529
saturation/100,
510530
brightness/100
511531
)

0 commit comments

Comments
 (0)