Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
assertColor
and assertSameColor
.
- Loading branch information
Juriy Zaytsev
committed
Apr 24, 2009
1 parent
5c1da60
commit 889a60d
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
(function(){ | ||
/** | ||
* @private | ||
* @method iterateData | ||
* @param {CanvasRenderingContext2D} ctx context to test | ||
* @param {Function} fn Callback, invoked with `currentValue`, `previousValue` and `index`. | ||
* Breaks out of the loop if callback returns `false`. | ||
*/ | ||
function iterateData(ctx, fn) { | ||
var data = ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height).data; | ||
for (var i = data.length; i--; ) { | ||
if (i > 4) { | ||
if (fn(data[i], data[i - 4], i) === false) break; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @method assertColor | ||
* @param {CanvasRenderingContext2D} ctx context to test | ||
* @param {String} color color in a hex value | ||
* @return {Boolean | null} `true` if all canvas pixels are of a given color, `null` if wrong color is given | ||
* @example `assertColor(canvas._oContextContainer, 'ff5555');` | ||
*/ | ||
function assertColor(ctx, color) { | ||
var match, r, g, b; | ||
if (match = String(color).match(/^#?([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/i)) { | ||
r = parseInt(match[1], 16); | ||
g = parseInt(match[2], 16); | ||
b = parseInt(match[3], 16); | ||
} | ||
else return null; | ||
var result = true; | ||
iterateData(ctx, function(currentValue, prevValue, i) { | ||
if ((!(i % 4) && (currentValue !== r)) || | ||
(!((i-1) % 4) && (currentValue !== g)) || | ||
(!((i-2) % 4) && (currentValue !== b))) { | ||
return (result = false); | ||
} | ||
}); | ||
return result; | ||
} | ||
|
||
/** | ||
* @method assertSameColor | ||
* @param {CanvasRenderingContext2D} ctx context to test | ||
* @return {Boolean} `true` if all canvas pixels are of the same color | ||
* @example `assertSameColor(canvas._oContextContainer);` | ||
*/ | ||
function assertSameColor(ctx) { | ||
var result = true; | ||
iterateData(ctx, function(currentValue, prevValue, i) { | ||
if (currentValue !== prevValue) { | ||
return (result = false); | ||
} | ||
}); | ||
return result; | ||
} | ||
|
||
// export as global | ||
this.assertColor = assertColor; | ||
this.assertSameColor = assertSameColor; | ||
})(); |