Skip to content

Commit

Permalink
fix(oklab): make sure color.set and color.get still work with oklab
Browse files Browse the repository at this point in the history
  • Loading branch information
gka committed Feb 3, 2022
1 parent 0d0c494 commit 93af49d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
8 changes: 4 additions & 4 deletions src/ops/get.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const Color = require('../Color');

Color.prototype.get = function(mc) {
const [mode,channel] = mc.split('.');
Color.prototype.get = function (mc) {
const [mode, channel] = mc.split('.');
const src = this[mode]();
if (channel) {
const i = mode.indexOf(channel);
const i = mode.indexOf(channel) - (mode.substr(0, 2) === 'ok' ? 2 : 0);
if (i > -1) return src[i];
throw new Error(`unknown channel ${channel} in mode ${mode}`);
} else {
return src;
}
}
};
31 changes: 20 additions & 11 deletions src/ops/set.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
const Color = require('../Color');
const {type} = require('../utils');
const { type } = require('../utils');

Color.prototype.set = function(mc, value, mutate=false) {
const [mode,channel] = mc.split('.');
Color.prototype.set = function (mc, value, mutate = false) {
const [mode, channel] = mc.split('.');
const src = this[mode]();
if (channel) {
const i = mode.indexOf(channel);
const i = mode.indexOf(channel) - (mode.substr(0, 2) === 'ok' ? 2 : 0);
if (i > -1) {
if (type(value) == 'string') {
switch(value.charAt(0)) {
case '+': src[i] += +value; break;
case '-': src[i] += +value; break;
case '*': src[i] *= +(value.substr(1)); break;
case '/': src[i] /= +(value.substr(1)); break;
default: src[i] = +value;
switch (value.charAt(0)) {
case '+':
src[i] += +value;
break;
case '-':
src[i] += +value;
break;
case '*':
src[i] *= +value.substr(1);
break;
case '/':
src[i] /= +value.substr(1);
break;
default:
src[i] = +value;
}
} else if (type(value) === 'number') {
src[i] = value;
Expand All @@ -31,4 +40,4 @@ Color.prototype.set = function(mc, value, mutate=false) {
} else {
return src;
}
}
};

0 comments on commit 93af49d

Please sign in to comment.