Github Project: https://github.com/billeatman/oauth2-Examples

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.

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:

t1037A344-5056-9852-3BF604C3F789A07E1
t1037A344-5056-9852-3BF604C3F789A07E2
t1037A344-5056-9852-3BF604C3F789A07E3
t1037A344-5056-9852-3BF604C3F789A07E4

struct
HELLO Hello World!

Intermittently I will get the following output:

tE65ACFC2-5056-9852-3BC66FCFAEE291C51
tE65ACFC2-5056-9852-3BC66FCFAEE291C52
tE65ACFC2-5056-9852-3BC66FCFAEE291C53
tE65ACFC2-5056-9852-3BC66FCFAEE291C54
[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.

1<!---
2Tested with the following
3CF Version: 9,0,1,274733
4Update Level: hf901-00008.jar --->

5
8
11
14</cffunction>
15
16<cfset variables.mystruct = "">
17
20    <cfset variables.mystruct = structNew()>
21    <cfset variables.mystruct.hello = "Hello World!">
23
26
29
32
34    <cfoutput>#i#</cfoutput><br />
35 <cfthread action="join" name="#trim(i)#" timeout="2000" / >
36</cfloop>
37
38<cfdump var="#mystruct#" >

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}$$

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?

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
BLUE
#0000FF (0, 0, 255)
WHITE
#FFFFFF (255, 255, 255)
FUCHSIA
#FF00FF (255, 0, 255)
MAROON
#800000 (128, 0, 0)
GREEN
#008000 (0, 128, 0)
AQUA
#00FFFF (0, 255, 255)
OLIVE
#808000 (128, 128, 0)
NAVY
#000080 (0, 0, 128)
SILVER
#C0C0C0 (192, 192, 192)
YELLOW
#FFFF00 (255, 255, 0)
BLACK
#000000 (0, 0, 0)
LIME
#00FF00 (0, 255, 0)
ORANGE
#FFA500 (255, 165, 0)
PURPLE
#800080 (128, 0, 128)
RED
#FF0000 (255, 0, 0)
GRAY
#808080 (128, 128, 128)
1<cfset qColors = getCSS21Colors(resultType: 'query')>
2<table>
4 <th>Name</th>
5 <th>Color</th>
6 <th>Hex</th>
7 <th>RGB</th>
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>
19</table>

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.

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)">
3
4 <cfset var retVal = structNew()>
5 <cfset var i = 0>
6 <cfset var o = 0>
7 <cfset var offset = 0>
8
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>
13
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>
21
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>
31
32 <cfset retVal.a = 255>
33
34 <cfreturn retVal>
35</cffunction>