Skip to content

Instantly share code, notes, and snippets.

@stevewithington
Last active April 20, 2016 22:38
Show Gist options
  • Save stevewithington/8203996 to your computer and use it in GitHub Desktop.
Save stevewithington/8203996 to your computer and use it in GitHub Desktop.
Mura CMS : Mura allows you to create an extended attribute to collect a color value via a color picker. The value is stored as: rgba(x,x,x,1). You can use a number of methods to extrapolate this value into other formats as needed. Keep in mind though, that most modern browsers allow for CSS3 color values (http://caniuse.com/css3-colors), so you …
<!--- drop these methods into your Site or Theme contentRenderer.cfm --->
<cfscript>
// props: http://cflib.org/udf/RGBtoHex
public any function RGBtoHex(r,g,b){
var hexColor = '';
var hexPart = '';
var i = 0;
for (i=1; i <= 3; i++){
hexPart = FormatBaseN(arguments[i],16);
if (Len(hexPart) == 1) {
hexPart = '0' & hexPart;
}
hexColor &= hexPart;
}
return hexColor;
}
// custom color parser
public any function parseMuraColor(muraColor='', colorFormat='', rgbaDefault='0,0,0,1') {
var c = {};
c.muraColor = arguments.muraColor;
c.rgba = Len(c.muraColor) ? REReplace(c.muraColor, 'rgba\(|\)', '', 'ALL') : arguments.rgbaDefault;
c.rgb = ListDeleteAt(c.rgba,4);
c.hex = $.RGBtoHex(ListGetAt(c.rgba,1), ListGetAt(c.rgba,2), ListGetAt(c.rgba,3));
return ListFindNoCase(StructKeyList(c), arguments.colorFormat) ? c[arguments.colorFormat] : c;
}
</cfscript>
<!--- Example Usage --->
#$.parseMuraColor(muraColor=$.content('someExtendedAttributeValue'), colorFormat='rgb')#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment