Getting started with oauth2 for Google

Github Project:

For CommandBox: forgebox install oauth2-Examples

I'm going to focus on how to set up the oauth2 project to work with Google. It's a confusing process if you've never done it before.

If you are new to oauth2, I would first read through the docs on Google.

Let's get started!

Our first step is to create a project using the Google Developers Console.


Intermittent Thread Issue

Update 02/23/2013 - Possible bug with server monitor.
This is now an open unverified bug with the server monitor.
Bug ID: 3498550
Just be aware that threads can behave unexpectedly when all server monitoring is turned on.

I ran into a cfthread issue last weekend, and maybe someone can tell me what is going on. The code listed below usually produces the following output:


HELLO Hello World!

Intermittently I will get the following output:

[empty string]

In the application.log, I get an error "tE65ACFC2-5056-9852-3BC66FCFAEE291C51: null"

I have only been able to recreate the problem with the server monitor turned on. I'm wondering if there is a problem with my code.

view plain print about
2Tested with the following
3CF Version: 9,0,1,274733
4Update Level: hf901-00008.jar --->

6<cfset threads = arrayNew(1)>
7<cfset threadPrefix = createUUID()>
9<cffunction name="addThreadName" returntype="string" access="private" output="false">
10    <cfset var threadName = "t" & variables.threadPrefix & arrayLenn(variables.threads) + 1)>
12    <cfset arrayAppend(variables.threads, threadName)>
13    <cfreturn threadName>
16<cfset variables.mystruct = "">
18<cfthread name="#addThreadName()#" action="run">
19    <cfthread action="sleep" duration="100" />
20    <cfset variables.mystruct = structNew()>
21    <cfset variables.mystruct.hello = "Hello World!">
24<cfthread name="#addThreadName()#" action="run">
27<cfthread name="#addThreadName()#" action="run">
30<cfthread name="#addThreadName()#" action="run">
33<cfloop array="#variables.threads#" index="i">
34    <cfoutput>#i#</cfoutput><br />
35 <cfthread action="join" name="#trim(i)#" timeout="2000" / >
38<cfdump var="#mystruct#" >

Mapping an RGB color to the nearest palette color in ColdFusion

RGB colors in 3d space graph

I ran into a need to map an RGB value to a palette color the last time I worked with the Google calendar API. I kept getting errors back when creating new calendars, and realized that I needed to pass a hex color that was in a defined palette. This left me with fewer options for the end-user, as I would now have to present them with a list of valid colors. I could avoid some type of palette chooser by mapping a given RGB value to the nearest Google color. In my situation an exact color match was not necessary.

Last week I posted an article with a function that would return the 17 CSS 2.1 colors. The code for the function was simple, but I needed it for this article. I actually thought about being cruel and writing the function to return the web safe color palette.

Mapping an RGB value to a palette color is a problem of finding the palette color that is the nearest in distance to a given RGB value. We can visualize this as shown on the left by plotting each palette color in 3d space using each color component as an axis.

We can get the Euclidean distance between two points by using the formula shown below that returns the distance between points P and Q.

$$ d(p,q) = \sqrt{(p_r - q_r)^2 + (p_g - q_g)^2 + (p_b - q_b)^2} $$


Is it finally time to embrace cfscript?

william eatman contemplating change

I feel like I've been a long hold out for not moving to cfscript. Sure I've used it, but I've always been a fan of the tag markup. My rationale has been that the tag language could do things that cfscript could not. I've never bought into the argument that cfscript would save me time, as time programming is usually spent problem solving and not actually typing the code.

Over the past week I have been looking at new open source CF projects and can see that many have embraced cfscript, so much so that my guess is that CF 9 will age much faster than CF 8 did. CF 10 does a good job filling in the holes cfscript had as well as adding advanced features like closures. In fact, it looks like I can now do more with cfscript than the tag markup. Maybe cfscript has grown up?

CSS 2.1 color palette function for ColdFusion

Yesterday I wrote a simple function for an upcoming post on working with defined color palettes. The palette of CSS 2.1 colors is small and useful for examples and could also be useful for form validation of a color field.

The following table was easily generated using GetCSS21Colors and HexToRGB from my previous post.

Name Color Hex RGB
#0000FF (0, 0, 255)
#FFFFFF (255, 255, 255)
#FF00FF (255, 0, 255)
#800000 (128, 0, 0)
#008000 (0, 128, 0)
#00FFFF (0, 255, 255)
#808000 (128, 128, 0)
#000080 (0, 0, 128)
#C0C0C0 (192, 192, 192)
#FFFF00 (255, 255, 0)
#000000 (0, 0, 0)
#00FF00 (0, 255, 0)
#FFA500 (255, 165, 0)
#800080 (128, 0, 128)
#FF0000 (255, 0, 0)
#808080 (128, 128, 128)
view plain print about
1<cfset qColors = getCSS21Colors(resultType: 'query')>
3 <thead>
4 <th>Name</th>
5 <th>Color</th>
6 <th>Hex</th>
7 <th>RGB</th>
8 </thead>
9 <cfoutput query="qColors">
10 <cfset myrgb = HexToRGB(color)>
11 <tr>
12 <td>#name#</td>
13 <td><div style="background: #color#; height: 20px; width: 100px;"></div></td>
14 <td>#color#</td>
15 <td>
16 (#myrgb.r#,#myrgb.g#,#myrgb.b#)</td>
17 </tr>
18 </cfoutput>


Fast Hex to RGB function for ColdFusion

Over Christmas I started working on a ColdFusion image library and wrote a nice hex to RGB function. I used a few tricks to make it as fast as possible. I'll write more about some of the optimizations in the coming week. Let me know if you can identify a way to make it even faster.

view plain print about
1<cffunction name="HexToRGB" access="public" output="false" returntype="struct" hint="hex to struct r,g,b,a">
2    <cfargument name="hex" type="string" required="true" hint="3 or 6 digit hex value (valid examples: ##FFF, FFF, ##FFFFFF, FFFFFF)">
4 <cfset var retVal = structNew()>
5 <cfset var i = 0>
6 <cfset var o = 0>
7 <cfset var offset = 0>
9    <!--- cheaper in time to look for a leading '#' and set an offset than burn a call to removeChars --->
10    <cfif mid(arguments.hex, 1, 1) EQ chr(35)>
11     <cfset offset = 1>
12 </cfif>
14    <!--- Remove non-hex values. the following is faster than regex --->
15     <cfloop from="#1 + offset#" to="#len(arguments.hex)#" index="i">
16     <cfif find(mid(arguments.hex, i + o, 1), '0123456789ABCDEFabcdef') EQ 0>
17     <cfset arguments.hex = removeChars(arguments.hex, i + o, 1)>
18     <cfset o = o - 1>
19     </cfif>
20 </cfloop>
22 <cfif len(arguments.hex) EQ (3 + offset)>
23 <cfset retVal.r = InputBaseN(mid(arguments.hex, 1 + offset, 1) & mid(arguments.hex, 1 + offset, 1), 16)>
24 <cfset retVal.g = InputBaseN(mid(arguments.hex, 2 + offset, 1) & mid(arguments.hex, 2 + offset, 1), 16)>
25 <cfset retVal.b = InputBaseN(mid(arguments.hex, 3 + offset, 1) & mid(arguments.hex, 3 + offset, 1), 16)>
26 <cfelse>
27 <cfset retVal.r = InputBaseN(mid(arguments.hex, 1 + offset, 2), 16)>
28 <cfset retVal.g = InputBaseN(mid(arguments.hex, 3 + offset, 2), 16)>
29 <cfset retVal.b = InputBaseN(mid(arguments.hex, 5 + offset, 2), 16)>
30 </cfif>
32 <cfset retVal.a = 255>
34 <cfreturn retVal>