00001 /* 00002 Copyright 2010 Pierre SCHWARTZ 00003 00004 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 00005 documentation files (the "Software"), to deal in the Software without restriction, including without 00006 limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 00007 of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following 00008 conditions: The above copyright notice and this permission notice shall be included in all copies or 00009 substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00010 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 00011 PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00012 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF 00013 OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00014 */ 00015 00023 #include "scriptedcolormaker.h" 00024 00025 scriptedColorMaker::scriptedColorMaker(int nbColors, QString paletteName):colorMaker(nbColors){ 00026 QString name("data/palettes/"); 00027 name.append(paletteName); 00028 name.append(".js"); 00029 00030 evaluatedFile = evaluateFile(engine, name); 00031 scriptedFunction = evaluatedFile.property("compute"); 00032 00033 QScriptValue constructor = evaluatedFile.property("init"); 00034 constructor.call(evaluatedFile,QScriptValueList() << nblevels); 00035 } 00036 00037 scriptedColorMaker::~scriptedColorMaker(){ 00038 } 00039 00043 QColor scriptedColorMaker::get(int level){ 00044 long result = scriptedFunction.call(evaluatedFile,QScriptValueList() << level << nblevels).toNumber(); 00045 00046 unsigned char r = result >> 16; 00047 unsigned char g = (result-(r << 16)) >> 8; 00048 unsigned char b = result-(r<<16) - (g<<8); 00049 00050 return QColor(r, g, b); 00051 } 00052 00056 QScriptValue scriptedColorMaker::evaluateFile(QScriptEngine &engine, QString const &filename){ 00057 QFile file(filename); 00058 file.open(QIODevice::ReadOnly); 00059 00060 return engine.evaluate(file.readAll(),filename); 00061 }