Last active
March 11, 2020 16:32
-
-
Save JamoCA/d2814588b6f518c72713becb3625c5bb to your computer and use it in GitHub Desktop.
Extract Color Palette using ColdFusion and ImageMagick (3/11/2020)
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
<!--- 20200311 Extract Color Palette using ColdFusion and ImageMagick | |
USAGE: extractPalette(filepath, numberOfColors) | |
GIST: https://gist.github.com/JamoCA/d2814588b6f518c72713becb3625c5bb | |
BLOG: https://dev.to/gamesover/generating-color-palette-using-coldfusion-imagemagick-25bp | |
---> | |
<cffunction name="extractPalette" returntype="array" output="no"> | |
<cfargument name="filepath" type="string" required="true"> | |
<cfargument name="numberOfColors" type="numeric" default="5" required="true"> | |
<cfargument name="EXEpath" default="c:\ImageMagick\convert.exe" type="string" required="true"> | |
<cfset var Temp = {raw="", response=[]}> | |
<cfexecute name="#arguments.EXEpath#" arguments="#arguments.filepath# -resize 300x +dither -colors #VAL(Arguments.numberOfColors)# -define histogram:unique-colors=true -format ""%c"" histogram:info:" variable="Temp.Raw" timeout="5"></cfexecute> | |
<cfscript> | |
Temp.Colors = ReMatch("##([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})", Temp.Raw); | |
for (Temp.color in Temp.Colors){ | |
for (temp.i=ArrayLen(Temp.Colors); temp.i GT 0; temp.i=temp.i-1){ | |
if (arrayLen(temp.response) GTE arguments.numberOfColors){break;} | |
arrayAppend(temp.response, Temp.Colors[temp.i]); | |
} | |
} | |
</cfscript> | |
<cfreturn Temp.response> | |
</cffunction> | |
<cfset timeStart = GetTickCount()> | |
<cfset Colors = extractPalette(imagePath, 5)> | |
<cfoutput> | |
<div><b>Time:</b> #NumberFormat(GetTickCount()-TimeStart)#ms</div> | |
<cfloop array="#Colors#" index="thisColor"> | |
<div><span style="background-color:#ThisColor#; min-width:50px; display:inline-block;"> </span> #thisColor#</div> | |
</cfloop> | |
</cfoutput> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment