First up we’ll query the colortable created in Updating parse.com, using the parse REST API. We can’t use the javaScript API in dome way from GAS, because it’s not caja compliant – if it were, we might be able to approach it in the same way as in Updating parse.com.
Nevertheless it’s pretty straightforward – here’s the parse specific code. The colors can be selected by scheme, label, name, code and key parameters.
// -- parse specific /** * creates a new object to interact with parse.com * @param {object} keys the parse.com .applicationId & . restKey * @return {cParseCom} The object */ function cParseCom(keys) { this.xKeys = keys; this.keys = function () { return this.xKeys; } return this; } /** * creates headers needed to authorize parse.com * @return {string} The headers */ cParseCom.prototype.restHeaders = function () { return { headers: { "X-Parse-Application-Id" : this.keys().applicationId, "X-Parse-REST-API-Key" : this.keys().restKey } }; } function getFromParse(e) { var results; results= { results: new cParseCom(getKeys()).queryArray("ColorTable",makeQuery(e), e.parameter.limit,e.parameter.useCache) }; return results; } /** * gets a JSON string response from parser.com * @param {string} class the parse.com class name * @param {object=} optQuery the object with the query constraint key/value pairs * @param {number} optLim limit * @param {boolean} optCache useCache * @return {Array} the results */ cParseCom.prototype.queryArray = function(class,optQuery,optLim,optCache) { // this is the max I'll take in one go to be compatible with parse.com var maxLimit = 1000; var q ,limit = mcpher.fixOptional(optLim,0), useCache = mcpher.fixOptional (optCache, true); var results =[]; var options = this.restHeaders(); while(true) { q = "?limit=" + (limit > 0 ? limit-results.length : maxLimit); q+= "&skip=" + results.length; q+= "&where="+mcpher.URLEncode(JSON.stringify(optQuery)); qr = new mcpher.cBrowser().get("https://api.parse.com/1/classes/" + class + q, options, useCache); qo = JSON.parse(qr); if (qo.results.length <= 0 ) return results; for (var i=0; i < qo.results.length && (limit==0 || results.length < limit);i++ ) { results.push(qo.results[i]); } } };
It needs a few basic utility functions as used all over on this site from the mcpher shared library, and you would also need to create a function to get your own parse.com keys that would look something like this
function getKeys() { return { applicationId : "a..whatever..8", restKey : "u..whatever...T"} ; }
The results
// get color data from colortable stored in parse.com // will return json or jsonp. // typical usage // ?key|rgb|hex=something&closest=n&scheme=s will return the 'n' closest colors in scheme x to the given target // ?scheme|key|name|label|code|rgb|hex=s&limit=n will return up to n colors as per the filter // ??key|rgb|hex=something&palette=n // &scheme=|s&type=hue|saturation|lightness&model=hsl|lch // will return a palette (within scheme if specified) // in each case, various color model properties are returned for each one function doGet(e) { return ContentService .createTextOutput(pit(e,getColorStuff(e))) .setMimeType(ContentService.MimeType.JSON); } function pit(e,s) { // jsonp? return e.parameter.callback ? e.parameter.callback + "(" + s + ");" : s; } function printStuff(){ Logger.log(getColorStuff()); } // get results and convert to json function getColorStuff(e) { var ea = argAlign(e); return JSON.stringify({ p: ea, c: processColorRequest(ea) } ); } function getTargetRgb(e) { var rgb,error,results; if (e.parameter.access == "key") { results = getSomehow (e,e.parameter.access); if (!results.results.length) { error = "could not find key " + e.parameter.key; } else { rgb = mcpher.htmlHexToRgb(results.results[0].hex); } } else { if(e.parameter.access == "rgb") rgb = e.parameter.rgb ; else if (e.parameter.access == "hex") rgb = mcpher.htmlHexToRgb(e.parameter.hex); else error = "access type invalid for closest match " + e.parameter.access ; if (!error) { results = {}; results.results = []; results.results.push ( { hex: mcpher.rgbToHTMLHex(rgb)} ); } } return { rgb: rgb, error: error , results: results } ; } function getFromSomewhere(e) { if (e.parameter.access == "hex") return {results:[{hex:e.parameter.hex}]}; else if (e.parameter.access == "rgb") return {results:[{ hex: mcpher.rgbToHTMLHex(e.parameter.rgb)}]}; else return e.parameter.provider == "parse" ? getFromParse(e) : getFromScriptDb(e) ; } function limitArray(a,n) { var results =[]; for (var i=0;i<n && i < a.length ; i++) results.push(a[i]); return results; } function processColorRequest(e) { var results,limited; if (e.parameter.action == "closest") { var rgb = getTargetRgb(e); if (!rgb.error) { // take the first 'n' sorted colors plus the original query var scheme = getSomehow (e,"scheme").results; var a = sortClosestColors ( rgb.rgb, scheme); limited = [rgb.results.results[0]].concat(limitArray(a,e.parameter.closest)); } results = { results : limited , error:rgb.error }; } else { results = getFromSomewhere(e); } return formatColorRequest(e, results); } // put all results to array function formatColorRequest(e, results) { var propArray = []; // return the original if (results.results) { for (var i = 0; i < results.results.length; i++ ) { var r = results.results[i]; try { propArray.push( {colortable:r, properties: mcpher.makeColorProps (mcpher.htmlHexToRgb(r.hex))}); } catch(err) { propArray.push( {colortable:r,properties:null, error: "invalid color " + err} ); } } } else propArray.push( {error:"no results",properties:null,colortable:null} ); return propArray; } function makeQuery(e) { var eArgs = e.parameter; var model = ["key","scheme","code","label","name"]; // build query var query; for (var k=0; k < model.length ; k++ ) { if (eArgs[model[k]]) { query = query || {}; query[model[k]] = eArgs[model[k]]; } } return query; } function getSomehow (e,k) { var result, ob = {}; ob[k] = e.parameter[k]; // get a single record based on a single property if (e.parameter.provider == "parse") { ob[k] = e.parameter[k]; result = new cParseCom(getKeys()).queryArray("ColorTable", ob,0,e.parameter.useCache); } else { result = mcpher.scriptDbSilo("colorSchemes",publicStuffDb()) .queryArray(ob) ; } return { results: result }; } function timeIt() { var scheme = "dulux"; var provider = "parse"; var e = {parameter:{provider:provider,scheme:scheme,useCache:false}}; mcpher.useTimer("scheme").start("Using GAS to getting scheme " + e.parameter.scheme + " from " + e.parameter.provider); var r = getSomehow(e,"scheme"); mcpher.useTimer("scheme").stop(); Logger.log("found "+r.results.length+" rows in scheme " + e.parameter.scheme + " using provider " + e.parameter.provider); Logger.log(mcpher.useTimer().report(false)); } function sortClosestColors (targetRgb, schemeArray) { var targetProps = mcpher.makeColorProps (targetRgb); for (var i= 0; i < schemeArray.length ; i++) { schemeArray[i].comparison = mcpher.compareColors (targetRgb, mcpher.htmlHexToRgb(schemeArray[i].hex)); } return schemeArray.sort (comparisonSort); } function comparisonSort(a,b) { if (a.comparison < b.comparison) return -1; if (a.comparison > b.comparison) return 1; return 0; } // set the arg defaults function argAlign(e) { eArgs = e || {}; eArgs.parameter = eArgs.parameter || {}; // testing //eArgs.parameter.hex = "#282821"; //eArgs.parameter.rgb = 1234; //eArgs.parameter.closest = 5; //eArgs.parameter.scheme = "htm"; //eArgs.parameter.key = "dulux-crushed raspberry"; //eArgs.parameter.provider = "parse"; //eArgs.parameter.limit = 0; eArgs.parameter.action = eArgs.parameter.action || (eArgs.parameter.hasOwnProperty("closest") ? "closest" : (eArgs.parameter.hasOwnProperty("palette") ? "palette" : "get" )); eArgs.parameter.access = eArgs.parameter.access || (eArgs.parameter.hasOwnProperty("key") ? "key" : (eArgs.parameter.hasOwnProperty("hex") ? "hex" : (eArgs.parameter.hasOwnProperty("rgb") ? "rgb" : "multi" ))); eArgs.parameter.cache = mcpher.LCase(eArgs.parameter.cache) || "yes"; eArgs.parameter.useCache = eArgs.parameter.cache == "yes"; eArgs.parameter.limit = eArgs.parameter.limit || "0"; eArgs.parameter.provider = eArgs.parameter.provider || "scriptdb"; eArgs.parameter.type = eArgs.parameter.type || "hue"; eArgs.parameter.closest = eArgs.parameter.closest || 5; eArgs.parameter.palette = eArgs.parameter.palette || 5; eArgs.parameter.model = eArgs.parameter.model || "lch"; return eArgs; }
https://script.google.com/macros/s/AKfycbzSdgK85uGHdQ9m076QkPV0B9a2kkgh7JHDmV8kzRgtkriSIwTn/exec?limit=2&scheme=dulux&provider=parse
For more about parse.com, see Parse.com
For help and more information join our forum, follow the blog or follow me on Twitter