/*
Copyright (c) 2012 Cem Kalyoncu
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
//color lists, they are not required by the functions, just for reference
static unsigned int bodycolors[]={
0xfff3c3a3,
0xffffd3b3,
0xffffe3c3,
0xfffee3d3,
0xfff0e8d8,
0xfff0b393,
0xffeece84,
0xffeede84,
0xff806030,
0xff604224,
//0xff306030, //fantasy stuff
//0xff50e080,
//0xff804080,
};
static unsigned int eyecolors[]={
0xff493929,
0xff50ecd4,
0xff50d4ec,
0xffb0c084,
};
static unsigned int haircolors[]={
0xff221009,
0xffa77423,
0xffb7a423,
0xffd7a443,
0xffa74423,
0xffffeeb0,
};
//clips a value between 0-255 to prevent overflowing
unsigned char clip(double v) {
return (unsigned char)std::min(std::max(v+0.5,0.),255.);
}
//These are class neutral functions which require the following members of canvas to exist, you may modify the code
//as necessary
// * int GetWidth() / int GetHeight()
// * T_ operator ()(int x, int y)
// T_ should support lower case ARGB members
//These functions can be implemented using HLS or better yet LCh color modes, however, they will be slower and
//will require additional library. If you do require it, I have the necessary equipment and I will be happy to help.
//Switches body color, both skin and eye. If your model is different than base, you may need to change base colors.
//Notice that this will work for blue or green eyes, otherwise it would be harder to detect eyes
template<class C_>
void BodyColorSwitch(C_ &canvas, unsigned int bodytarget, unsigned int eyetarget, unsigned int bodybase=0xfff3c3a3, unsigned int eyebase=0xff50d4ec) {
//extract base color components
double cr=(bodybase/0x10000)%0x100,cg=(bodybase/0x100)%0x100,cb=(bodybase/0x1)%0x100;
//calculate old-to-new ratio
cr=(bodytarget/0x10000)%0x100 / cr;
cg=(bodytarget/0x100)%0x100 / cg;
cb=(bodytarget/0x1)%0x100 / cb;
//same for the eyes
double ecr=(eyebase/0x10000)%0x100,ecg=(eyebase/0x100)%0x100,ecb=(eyebase/0x1)%0x100;
ecr=(eyetarget/0x10000)%0x100 / ecr;
ecg=(eyetarget/0x100)%0x100 / ecg;
ecb=(eyetarget/0x1)%0x100 / ecb;
//for all pixels, determined by GetHeight and GetWidth functions, change them as you need
for(int y=0;y<canvas.GetHeight();y++) {
for(int x=0;x<canvas.GetWidth();x++) {
//In skin color r>>g and r>>b
if(canvas(x,y).r>canvas(x,y).g+4 && canvas(x,y).r>canvas(x,y).b+5) {
canvas(x,y).r=clip(cr*canvas(x,y).r);
canvas(x,y).g=clip(cg*canvas(x,y).g);
canvas(x,y).b=clip(cb*canvas(x,y).b);
}
//if g>>>r or b>>>r we probably have an eye color
else if(canvas(x,y).r+10<canvas(x,y).g || canvas(x,y).r+10<canvas(x,y).b) {
canvas(x,y).r=clip(ecr*canvas(x,y).r);
canvas(x,y).g=clip(ecg*canvas(x,y).g);
canvas(x,y).b=clip(ecb*canvas(x,y).b);
}
//rest is the white parts of the eyes
}
}
}
//This function switches the hair color (can be another part as which will totally be recolored), as before base color is important
//it is designed for the blonde hair wulax had provided
template<class C_>
void HairColorSwitch(C_ &canvas, unsigned int target, unsigned int base=0xffa77423) {
//same as body color switch except without restrictions
double cr=(base/0x10000)%0x100,cg=(base/0x100)%0x100,cb=(base/0x1)%0x100;
cr=(target/0x10000)%0x100 / cr;
cg=(target/0x100)%0x100 / cg;
cb=(target/0x1)%0x100 / cb;
for(int y=0;y<canvas.GetHeight();y++) {
for(int x=0;x<canvas.GetWidth();x++) {
canvas(x,y).r=clip(cr*canvas(x,y).r);
canvas(x,y).g=clip(cg*canvas(x,y).g);
canvas(x,y).b=clip(cb*canvas(x,y).b);
}
}
}
LyoKQ29weXJpZ2h0IChjKSAyMDEyIENlbSBLYWx5b25jdQoKUGVybWlzc2lvbiBpcyBoZXJlYnkgZ3JhbnRlZCwgZnJlZSBvZiBjaGFyZ2UsIHRvIGFueSBwZXJzb24gb2J0YWluaW5nIGEgY29weSBvZgp0aGlzIHNvZnR3YXJlIGFuZCBhc3NvY2lhdGVkIGRvY3VtZW50YXRpb24gZmlsZXMgKHRoZSAiU29mdHdhcmUiKSwgdG8gZGVhbCBpbgp0aGUgU29mdHdhcmUgd2l0aG91dCByZXN0cmljdGlvbiwgaW5jbHVkaW5nIHdpdGhvdXQgbGltaXRhdGlvbiB0aGUgcmlnaHRzIHRvCnVzZSwgY29weSwgbW9kaWZ5LCBtZXJnZSwgcHVibGlzaCwgZGlzdHJpYnV0ZSwgc3VibGljZW5zZSwgYW5kL29yIHNlbGwgY29waWVzIG9mCnRoZSBTb2Z0d2FyZSwgYW5kIHRvIHBlcm1pdCBwZXJzb25zIHRvIHdob20gdGhlIFNvZnR3YXJlIGlzIGZ1cm5pc2hlZCB0byBkbyBzbywKc3ViamVjdCB0byB0aGUgZm9sbG93aW5nIGNvbmRpdGlvbnM6CgpUaGUgYWJvdmUgY29weXJpZ2h0IG5vdGljZSBhbmQgdGhpcyBwZXJtaXNzaW9uIG5vdGljZSBzaGFsbCBiZSBpbmNsdWRlZCBpbiBhbGwKY29waWVzIG9yIHN1YnN0YW50aWFsIHBvcnRpb25zIG9mIHRoZSBTb2Z0d2FyZS4KClRIRSBTT0ZUV0FSRSBJUyBQUk9WSURFRCAiQVMgSVMiLCBXSVRIT1VUIFdBUlJBTlRZIE9GIEFOWSBLSU5ELCBFWFBSRVNT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