Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created June 23, 2021 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennadel/e4f60bde644c9ff94f5e2684276bbfc0 to your computer and use it in GitHub Desktop.
Save bennadel/e4f60bde644c9ff94f5e2684276bbfc0 to your computer and use it in GitHub Desktop.
Generating Color Swatches With GraphicsMagick And Lucee CFML 5.3.7.47
<cfscript>
hexValues = [ "E63946", "F1FAEE", "A8DADC", "457B9D", "1D3557", "001219" ];
swatches = hexValues.map(
( hexValue ) => {
var imageFile = "./images/#hexValue#.png";
gm([
"convert",
// Since we're not manipulating an existing image, we have to explicitly
// define the size of the new image that we're creating.
"-size 400x400",
// The foundation of our swatch image will be a solid-colored background.
// For this, we can use the "XC" (Constant image uniform color) pseudo-
// image formats. XC will paint the given solid color onto the canvas.
"xc:###hexValue#",
// Draw the box for our HEX color label in the bottom-left corner of the
// canvas.
"-fill ##ffffff",
"-draw 'roundRectangle 3,370 86,397 1,1'",
// Draw the HEX color onto the label. To choose a specific font, we can
// link directly to the True Type Font file (which I downloaded for free
// from Google Fonts).
"-font #quote( expandPath( './Roboto Mono/RobotoMono-VariableFont_wght.ttf' ) )#",
"-fill ##262626",
"-pointsize 18",
"-draw 'text 12,391 #quote( hexValue )#'",
// Output the swatch image file using the PNG32 writer.
"PNG32:#quote( expandPath( imageFile ) )#"
]);
return({
hex: hexValue,
imageUrl: imageFile
});
},
// Parallel iteration.
true,
// Maximum number of threads.
3
);
```
<cfoutput>
<cfloop value="swatch" array="#swatches#">
<img
src="#encodeForHtmlAttribute( swatch.imageUrl )#"
alt="Color swatch for ###encodeForHtmlAttribute( swatch.hex )#."
title="Swatch: ###encodeForHtmlAttribute( swatch.hex )#"
/>
</cfloop>
</cfoutput>
```
// ------------------------------------------------------------------------------- //
// ------------------------------------------------------------------------------- //
/**
* I execute the given options against the GM (GraphicsMagick) command-line tool. If
* there is an error, the error is dumped-out and the processing is halted. If there
* is no error, the standard-output is returned.
*
* NOTE: Options are flattened using a space (" ").
*/
public string function gm(
required array options,
numeric timeout = 5
) {
execute
name = "gm"
arguments = options.toList( " " )
variable = "local.successResult"
errorVariable = "local.errorResult"
timeout = timeout
;
// If the error variable has been populated, it means the CFExecute tag ran into
// an error - let's dump-it-out and halt processing.
if ( len( errorResult ?: "" ) ) {
dump( errorResult );
abort;
}
return( successResult ?: "" );
}
/**
* A simple utility function that wraps the given value in double-quotes. This makes
* the command-line arguments a bit easier to read.
*
* NOTE: I would have loved to figure out how to ESCAPE embedded quotes within the
* value; but that appears to be a rather complicated issue command-line issue that I
* don't know how to solve. Thankfully, none of my inputs have quotes in them.
*/
public string function quote( required string value ) {
return( '"#value#"' );
}
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment