MattStypa.com
26Sep/11

reCAPTCHA for punBB 1.1 released

A small bug has been fixed.

Head over to reCAPTCHA for punBB page for more information.

Filed under: Code, Web Comments Off
20Sep/11

reCAPTCHA for punBB 1.0.0 released

reCAPTCHA for punBB 1.0.0 has been released.

This extension for punBB integrates google's reCAPTCHA system to minimize spam on the forum.

Head over to reCAPTCHA for punBB page for more information.

Filed under: Code, Web Comments Off
5Jan/11

Converting CSS colors to grayscale

Last week I was working on integrating punBB into my website and I ran into a small challenge. I needed to make punBB match my blog's color scheme. By default punBB looks like this:

I started editing the CSS file and quickly realized that it is going to take much more time and effort that I was willing to invest at that time. I started analyzing alternative solutions and quickly figured out that converting the default color scheme to grayscale would do the trick. I started looking online for a tool that could do such a thing. I didn't find anything but it didn't seem too hard to write a script that would change all the colors in a css file. Having absolutely no idea how to convert an RGB code from color to grayscale I setup my first experiment.

If RGB code describes intensity of red, green, and blue respectively, if I total the intensity values and then spread them evenly between red, green, and blue I should get gray of the same intensity as the base color. With that assumption in mind, I wrote the following VBScript code. I realize that it's dirty and could be written much better but I had 5 minutes to write it and it worked so don't wine.

'Setting up System File Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Opening input css file. 1 = reading
Set inFile = objFSO.OpenTextFile("in.css", 1)
'Opening output css file. 2 = writing
Set outFile = objFSO.OpenTextFile("out.css", 2)

'Main loop
Do Until inFile.AtEndOfStream
	'Read a line from input file
	line = inFile.ReadLine
	'Find position of first "#"
	pos = InStr(line,"#")
	'If "#" was found pos will be larger then 0. The reason this is a while loop is that "#" could occur more then once per line
	while pos > 0
		'Checking if the "#" found could be a 3 digit color code
		if mid(line,pos+4,1)=" " or mid(line,pos+4,1)=";" then
			'Converting rgb values to decimal from hex
			r = CInt("&H" & mid(line,pos+1,1))
			b = CInt("&H" & mid(line,pos+2,1))
			g = CInt("&H" & mid(line,pos+3,1))
			'This is where the conversion to grayscale happens
			t = hex((r+b+g)/3)
			'Rebuilding the line with the new value
			line = mid(line,1,pos) & t & t & t & mid(line,pos+4)

		'Checking if the "#" found could be a 6 digit color code
		elseif mid(line,pos+7,1)=" " or mid(line,pos+7,1)=";" then
			'Converting rgb values to decimal from hex
			r = CInt("&H" & mid(line,pos+1,2))
			b = CInt("&H" & mid(line,pos+3,2))
			g = CInt("&H" & mid(line,pos+5,2))
			'This is where the conversion to grayscale happens
			t = hex((r+b+g)/3)
			'Making sure that t is two digits long and if not add "0" in front
			if t < 2 then
				t = "0" & t
			end if
			'Rebuilding the line with the new value
			line = mid(line,1,pos) & t & t & t & mid(line,pos+7)
		end if
		'Checking for another "#" on this line
		pos = InStr(pos+1,line,"#")
	wend
	'Write to the output file
	outFile.WriteLine(line)
Loop
'Closing files
inFile.Close
outFile.Close
'Done
msgbox("Done")

Amazingly it worked like a charm so enjoy it.

Filed under: Code, Web Comments Off
29Jun/10

Tabs in TEXTAREA

codeI was working on a small personal project the other day and I ran into a surprising obstacle. I wanted an online form to behave just like notepad does. Specifically I wanted to use tabs in a textarea of a form. The shocking part was how scarce the information on the issue was and how bizarre some approaches were. I ended up writing my own JavaScript to handle this issue and realized that my code works so well that I need to put it out there for others to use. You can check it out in action HERE.

Tested with: IE6, IE7, IE8, FireFox, Chrome, Safari, and Opera

Here is the business end that goes into the "HEAD" section.

<script type="text/javascript">
	// This script allows intuitive TAB functionality in HTML form fields.
	// Due to browser incompatibilities the following code has to be included in the field tags:
	// onkeydown="checkTab(event)"; onkeypress="operaTab(event)";
	// visit MattStypa.com for more information

	function checkTab(event)
	{
		// First we check if the key pressed was a tab
		if (event.keyCode == 9)
		{
			// Is this IE?
			if (navigator.appName == 'Microsoft Internet Explorer')
			{
				// This stops the default behavior of the tab key. preventDefault does not work in IE.
				event.returnValue = false;
				// This gets the current selection or cursor location
				range = document.selection.createRange();
				// Replaces the current selection with a tab character or simply inserts it at the current cursor position
				range.text = "\t";
				// This resets the selection
				range.collapse(true);
				// And this executes above statement.
				range.select();
			}
			else
			{
				// This stops the default behavior of the tab key
				event.preventDefault();
				// We need to save the current position of the cursor.
				var c = event.target.selectionStart;
				// Inserting a tab into the string.
				// First we grab what comes before the current selection (or cursor position).
				// Then we add the tab character. Finally we add the part the comes after the
				// current selection (or cursor position).
				event.target.value = event.target.value.slice(0,event.target.selectionStart).concat("\t").concat(event.target.value.slice(event.target.selectionEnd,event.target.value.length));
				// This sets the cursor position.
				event.target.selectionStart = c + 1;
				// And this cancels selection. Opera requires that this is on a seperate line.
				event.target.selectionEnd = c + 1;
			}
		}
	}

	function operaTab(event)
	{
		if (navigator.appName == 'Opera' && event.keyCode == 9)
		{
				// This stops the default behavior of the tab key
				// Opera is unable to do that when called from onkeydown
				event.preventDefault();
		}
	}
</script> 

Also we need to map some events to the field.

<textarea onkeydown="checkTab(event)"; onkeypress="operaTab(event)";></textarea>
Filed under: Code, Web Comments Off
20Mar/10

WordPress

wordpress logoIt seems that writing about an amazing blogging system is quite appropriate for a first post on my, well, blog. The one I chose is called WordPress. At first I was skeptical about it and a visit to WordPress.com did not convince me to try it. I thought that it is just another hosted service such as Blogger. My good friend enlightened me and pointed to WordPress.org where I learned what it truly is. Essentially, WordPress is an Open-Source PHP script that, together with MySQL, provides every feature a blogger might desire.

I fell in love with this system right away. This truly is what I have been looking for. And I am not the only one to think that. WordPress is used by more then 200 million websites worldwide and is consistently gaining more supporters. You may ask, "What makes it so much better then anything else out there?" Lets take a look.

Besides the standard features you would find in other blogging system, WordPress has support for Themes and Plugins that expend its looks and functionality. One of the cool things about those is that they can be installed directly from the control panel. You don't need to upload any files through ftp anymore. On top of that WordPress is so flexible that it is also commonly used as CMS or even a photo gallery.

Multiple tags and categories can be assigned to your posts. Those in return are displayed in an appropriate widget and help visitors find related or other interesting topics on your blog. Widgets are like building blocks that can be placed, organized, and customized to fit your own needs. Some of these include  Archives, Links, Pages, and even a Tag Cloud.

WordPress also features an amazing Media Library that allows you to easily upload your images. It even has a simple image editor build right in. This whole thing is simply stunning.

P.S: Welcome to my Blog

Filed under: Software, Web Comments Off