As I am digging into the Zxing barcode library in ColdFusion, one of many choices that I’ve it set the On/Off colours for the pixels inside the rendered picture. Every of those colours is represented as a single integer by which the Alpha, Purple, Inexperienced, and Blue channels of the colour are encoded into the 4 bytes of the 32-bit integer, respectively. ColdFusion makes this a straightforward process with its inputBaseN()
operate, which permits us to effortlessly convert numbers between completely different radix.
To discover this system, let’s use Java’s java.awt.Coloration
class. This class may be instantiated utilizing varied constructor signatures. And for this exploration, we’ll use these two:
Coloration( int r, int g, int b, int a )
Coloration( int rgba, boolean hasalpha )
The primary constructor name will act as our management. Since we’re passing-in the person channels, we all know that we cannot have to fret about bit-mechanics. Then, the second constructor name will act as our take a look at, to see if we will generate the identical colours utilizing a single integer (by which every byte represents a unique channel).
Here is the ColdFusion code that makes each calls after which compares the output:
<cfscript>
hexColor = "ff3366aa"; // {R,G,B,A}
pink = hexColor.mid( 1, 2 );
inexperienced = hexColor.mid( 3, 2 );
blue = hexColor.mid( 5, 2 );
alpha = hexColor.mid( 7, 2 );
// Creating colours utilizing particular person RGBA channels.
colorFromChannels = createObject( "java", "java.awt.Coloration" ).init(
inputBaseN( pink, 16 ),
inputBaseN( inexperienced, 16 ),
inputBaseN( blue, 16 ),
inputBaseN( alpha, 16 )
);
// Creating colours with a single INT by which every of the 4 bytes (inside the INT)
// represents Alpha, Purple, Inexperienced, and Blue, respectively. Discover that we have now to change
// the place the Alpha byte is situated: within the unique Hex string it was on the finish
// (which is what we use on the internet); however, the INT-input wants it to be at the beginning.
colorFromInt = createObject( "java", "java.awt.Coloration" ).init(
inputBaseN( "#alpha##pink##inexperienced##blue#", 16 ),
true // Has alpha.
);
dump(
label = "From Channels",
var = [
colorFromChannels.getRed(),
colorFromChannels.getGreen(),
colorFromChannels.getBlue(),
colorFromChannels.getAlpha()
]
);
dump(
label = "From HEX Int",
var = [
colorFromInt.getRed(),
colorFromInt.getGreen(),
colorFromInt.getBlue(),
colorFromInt.getAlpha()
]
);
</cfscript>
One caveat right here is that I outlined the unique enter hex as ff3366aa
, by which the alpha – aa
– is on the finish of the string. That is how colours work on the internet. Nevertheless, when representing the colour as an INT (on this context), the alpha channel must be on the opposite finish (aaff3366
). This is the reason I am utilizing string concatenation when defining the colorFromInt
variable.
And, after we run this ColdFusion code, we get the next output:

As you may see, the RGBA channels extracted from the Coloration cases are precisely the identical. We have been in a position to correctly signify the RGBA coloration as an INT by which every byte represented a unique coloration.
I am going to reveal this in a follow-up publish on QR Codes; however, within the Zxing library, which method permits me to simply create these On/Off colours:
<cfscript>
imageConfig = fromJars( "com.google.zxing.shopper.j2se.MatrixToImageConfig" )
.init(
inputBaseN( "ff0000CD", 16 ), // On coloration ARGB.
inputBaseN( "ffffffff", 16 ) // Off coloration ARGB.
)
;
</cfscript>
ColdFusion is so freaking candy!
Need to use code from this publish?
Take a look at the license.
https://bennadel.com/4807