Skip to content

Instantly share code, notes, and snippets.

@apipkin
Forked from mattparker/_getOffsetAndFinish.js
Created November 6, 2012 23:37
Show Gist options
  • Save apipkin/4028491 to your computer and use it in GitHub Desktop.
Save apipkin/4028491 to your computer and use it in GitHub Desktop.
idea for kb refactor of Y.Color.Harmony
/**
Generates a series of colors from startColor at various offsets
@protected
@method _getOffsetAndFinish
@param {String} startColor Start color
@param {Array} arrOffsets Array of colors
@param {String} [to]
@returns {Array} Of offsetted and finished colors
**/
_getOffsetAndFinish: function (startColor, arrOffsets, to) {
var ret = [],
i = 0,
c0 = Harmony._start(startColor),
to = to || Color.findType(startColor);
for (;i < arrOffsets.length; i = i + 1) {
ret.push(Harmony.finish(Harmony.getOffset(c0, {h: arrOffset[i]})), to);
}
return ret;
}
// so something like getTriad becomes something like
// ie quite a lot smaller kb wise
getTriad: function (str, to) {
return Harmony._getOffsetAndFinish(str, [0, TRIAD_OFFSET, -TRIAD_OFFSET], to);
}
/**
Takes an HSL array, and an array of offsets and returns and array
of colors that have been adjusted. The returned colors will
match the array of offsets provided. If you wish you have the
same color value returned, you can provide null or an empty
object to the offsets. The returned array will contain color
value strings that have been adjusted from subtractive to
additive.
@private
@method _adjustOffsetAndFinish
@param {Array} color
@param {Array} offsets
@param {String} to
@return {Array}
@since 3.8.0
**/
_adjustOffsetAndFinish: function(color, offsets, to) {
var colors = [],
i,
l = offsets.length,
_c;
for (i = 0; i < l; i++ ) {
_c = color.concat();
_c = Harmony.getOffset(_c, offsets[i]);
colors.push(Harmony._finish(_c, to));
}
return colors;
},
//// B E F O R E R E F A C T O R
/**
Returns an Array of three colors. The first color in the Array
will be the color passed in. The second two will be split
complementary colors.
@public
@method getSplit
@param {String} str
@param {Number} [offset]
@param {String} [to]
@returns {String}
**/
getSplit: function(str, offset, to) {
var c = Harmony._start(str),
c1,
c2;
offset = offset || SPLIT_OFFSET;
to = to || Color.findType(str);
c = Harmony.getOffset(c, {h: 180});
c1 = c.concat();
c1 = Harmony.getOffset(c1, {h: offset});
c2 = c.concat();
c2 = Harmony.getOffset(c2, {h: -offset});
// set base color back to original value
c = Harmony.getOffset(c, {h: 180});
return [
Harmony._finish(c, to),
Harmony._finish(c1, to),
Harmony._finish(c2, to)
];
},
/// A F T E R R E F A C T O R
/**
Returns an Array of three colors. The first color in the Array
will be the color passed in. The second two will be split
complementary colors.
@public
@method getSplit
@param {String} str
@param {Number} [offset]
@param {String} [to]
@return {String}
@since 3.8.0
**/
getSplit: function(str, offset, to) {
var c = Harmony._start(str),
offsets = [];
offset = offset || SPLIT_OFFSET;
to = to || Color.findType(str);
offsets.push({});
offsets.push({ h: 180 + offset });
offsets.push({ h: 180 - offset });
return Harmony._adjustOffsetAndFinish(c, offsets, to);
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment