Skip to content

Commit 1c0c107

Browse files
committed
update color_utils.js functions hsbaToHSLA and hslaToHSBA more readable
1 parent 4c457e1 commit 1c0c107

File tree

3 files changed

+60
-10
lines changed

3 files changed

+60
-10
lines changed

docs/js/p5.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! p5.js v0.4.7 August 15, 2015 */
1+
/*! p5.js v0.4.7 August 17, 2015 */
22
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.p5 = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
33

44
},{}],2:[function(require,module,exports){
@@ -9281,8 +9281,14 @@ p5.ColorUtils.hslaToHSBA = function(hsla) {
92819281
var l = hsla[2];
92829282
var a = hsla[3] || 1;
92839283

9284+
var v;
9285+
9286+
//Hue and Alpha stay the same
92849287
s *= l < 0.5 ? l : 1 - l;
9285-
return[ h, 2*s/(l+s), l+s, a];
9288+
v = l + s;
9289+
s = 2 * s / (l + s);
9290+
9291+
return[ h, s, v, a];
92869292
};
92879293

92889294
/**
@@ -9297,7 +9303,26 @@ p5.ColorUtils.hsbaToHSLA = function(hsba) {
92979303
var v = hsba[2];
92989304
var a = hsba[3] || 1;
92999305

9300-
return [ h, s*v/((h=(2-s)*v)<1?h:2-h), h/2, a];
9306+
//Hue and Alpha stay the same
9307+
//Lightness is (2 - s) * v / 2
9308+
var l = (2 - s) * v / 2;
9309+
9310+
//Saturation is very different between the two color spaces
9311+
//If l < 0.5 set it to s / (2 - s)
9312+
//Otherwise s * v / (2 - (2 - s) * v)
9313+
if( l !== 0 ){
9314+
if( l === 1 ){
9315+
s = 0;
9316+
}
9317+
else if( l < 0.5 ){
9318+
s = s / (2 - s);
9319+
}
9320+
else{
9321+
s = s * v / (2 - l * 2);
9322+
}
9323+
}
9324+
9325+
return [ h, s, l, a];
93019326
};
93029327

93039328
module.exports = p5.ColorUtils;

0 commit comments

Comments
 (0)