From fd1c56f988bd91958a186a7136f2f4865d466629 Mon Sep 17 00:00:00 2001 From: Jared Adams Date: Thu, 18 Jul 2013 18:38:10 -0600 Subject: Add image loader test to dye stuff. --- public/dye_channels.html | 37 ++++++++++++++++++++++++++++ public/images/channels.png | Bin 0 -> 2949 bytes public/js/mp/dye.js | 59 ++++++++++++++++++++++++--------------------- 3 files changed, 69 insertions(+), 27 deletions(-) create mode 100644 public/dye_channels.html create mode 100644 public/images/channels.png diff --git a/public/dye_channels.html b/public/dye_channels.html new file mode 100644 index 0000000..bdb74cb --- /dev/null +++ b/public/dye_channels.html @@ -0,0 +1,37 @@ + + + + +Mana Portal Playground + + + + + + + + diff --git a/public/images/channels.png b/public/images/channels.png new file mode 100644 index 0000000..d7a1fab Binary files /dev/null and b/public/images/channels.png differ diff --git a/public/js/mp/dye.js b/public/js/mp/dye.js index 65aa429..b9955f1 100644 --- a/public/js/mp/dye.js +++ b/public/js/mp/dye.js @@ -4,6 +4,7 @@ var mp = function(mp) { getChannel: getChannel, parseDyeString: parseDyeString, asDyeString: asDyeString, + updateColor: updateColor, dyeImage: dyeImage }; @@ -97,6 +98,36 @@ var mp = function(mp) { return dyeString; } + function updateColor(color, p) { + var channel = getChannel(color); + var channelId = channel.channel; + var intensity = channel.intensity; + + // If this is an unknown dye channel, an empty dye channel, not a pure color, or black, skip it + if (!channelId || !(channelId in dyeData) || !dyeData[channelId].length || intensity == 0) { + return; + } + + // Scale the intensity from 0-255 to the palette size (i is the palette index, t is the remainder) + var val = intensity * dyeData[channelId].length + var i = Math.floor(val / 255); + var t = val - i * 255; + + // If we exactly hit one of the palette colors, just use it + if (!t) { + --i; + color[p ] = dyeData[channelId][i][0]; + color[p + 1] = dyeData[channelId][i][1]; + color[p + 2] = dyeData[channelId][i][2]; + return; + } + + // If we're between two palette colors, interpolate between them (the first color in a palette is implicitly black) + color[p ] = Math.floor(((255 - t) * (i && dyeData[channelId][i - 1][0]) + t * dyeData[channelId][i][0]) / 255); + color[p + 1] = Math.floor(((255 - t) * (i && dyeData[channelId][i - 1][1]) + t * dyeData[channelId][i][1]) / 255); + color[p + 2] = Math.floor(((255 - t) * (i && dyeData[channelId][i - 1][2]) + t * dyeData[channelId][i][2]) / 255); + } + /* * Dye the internal image data based on the specification provided by dyeData. * The specification can be generated from a dyeString by parseDyeString. @@ -112,33 +143,7 @@ var mp = function(mp) { continue; } - var channel = getChannel(pixel); - var channelId = channel.channel; - var intensity = channel.intensity; - - // If this is an unknown dye channel, an empty dye channel, not a pure color, or black, skip it - if (!channelId || !(channelId in dyeData) || !dyeData[channelId].length || intensity == 0) { - continue; - } - - // Scale the intensity from 0-255 to the palette size (i is the palette index, t is the remainder) - var val = intensity * dyeData[channelId].length - var i = Math.floor(val / 255); - var t = val - i * 255; - - // If we exactly hit one of the palette colors, just use it - if (!t) { - --i; - imageData[p ] = dyeData[channelId][i][0]; - imageData[p + 1] = dyeData[channelId][i][1]; - imageData[p + 2] = dyeData[channelId][i][2]; - continue; - } - - // If we're between two palette colors, interpolate between them (the first color in a palette is implicitly black) - imageData[p ] = ((255 - t) * (i && dyeData[channelId][i - 1][0]) + t * dyeData[channelId][i][0]) / 255; - imageData[p + 1] = ((255 - t) * (i && dyeData[channelId][i - 1][1]) + t * dyeData[channelId][i][1]) / 255; - imageData[p + 2] = ((255 - t) * (i && dyeData[channelId][i - 1][2]) + t * dyeData[channelId][i][2]) / 255; + updateColor(imageData, p); } /* TODO */ return imageData; -- cgit v1.2.3-60-g2f50