Posted on Leave a comment

Javascript Controlled SVG Part 2

I have already covered utility functions and GUI code, but most of the code is for calculations of color data. This post will explain how all of the code ties together.

When the user first loads the color picker the default state is loaded. All values are zero and correspondingly the color is black. RGB (0,0,0) is black and HSV (0°,0,0) is black.

All event handlers do basically the same thing. First the master color color data  is updated to reflect the changes made by the user. Next the RGB, HSV, and hex code are updated to reflect the color data. If the user is changing RGB values, those copied to the color date and used to calculate HSV and hex code. If the user is changing HSV values, those copied to the color date and used to calculate RGB and hex code.

All event handlers ultimately call the update function. This in-turn updates the handle locations, background gradients, and preview area. each is separated into its own function.

The updateHandles function uses the color data to update the locations of the sliders. Let’s go over one group in the function.

var redHandle = max - colorData.red / 255 * 199;
document.getElementById("redHandle").setAttribute("y",redHandle);
document.getElementById("redLabel").firstChild.nodeValue = Math.round(colorData.red);

The first line is responsible for calculating the top side of the red value slider handle. Yes, I know it has magic numbers. if you read my earlier posts you would know this is more like a proof of concept than a finished product. magic numbers never make it into my finished works. 255 is the maximum value for red, 199 is for the size of the slider. Looking at it now, 199 could be replaced with (max - min). The second line moves the handle to the new position. The third updates the value displayed below the slider. These three lines are repeated for every slider with different constants.

The background of each slider shows the resulting color of moving that particular slider. These gradients need updated every time the color data changes. doing so is as easy as calculating color data as if the slider is set to each end. The hue background is slightly different. It has one gradient with all the hues represented and two overlays. one overlay is completely white representing the saturation. the other is black representing the value. The opacity of each overlay is adjusted according to the current location of the corresponding slider.

The preview is easier to compute than any topic discussed so far. Using the RGB color data, the hex code is calculated. Each color is converted using the deciToHexDigits function to ensure placement of zeros. The resulting string is then shown to the user and applied as the fill of the preview area.

Posted on Leave a comment

Javascript Controlled SVG Part 1

In my last post I described the experience and development of my first true attempt at SVG. This post will describe the javascript code which implements the behavior of that color picker. This is meant to describe the code to somebody who understands basic programming but not necessarily javascript or working with graphics. If you have a solid grasp on javascript and programming GUIs you can skip this post.

All code is viewable in the source of the page.

First let’s cover the “global” variables. The first, HEX, is used as a constant in the conversion from decimal to hexadecimal (more on that soon). The colorData variable is used to store the state of the color data and is directly modified and displayed by the sliders. The min and max variables are treated more like constants as they represent the minimum and maximum pixel locations of the sliders. The selected variable points to the currently selected slider, or null if none are selected. To be selected means the user is currently modifying that slider. Finally, button points to the button currently in use by the user, or null if none are in use.

Now for the functions. The code covered in this post does not include application logic. These functions facilitate communication between the graphical state and the data and most are event handlers. Most of them are fairly straightforward.

The first function is mouseCoords. This function is pretty straightforward. It takes any mouse event fired by the browser, extracts the coordinates of the event, and returns a single object containing the x and y coordinates.

deciToHex takes an integer and returns a string of the hexadecimal representation. The deciToHexDigits function converts an integer to a zero padded hex string which has a minimum number of digits. It takes two parameters, the integer to convert and the number of digits.

The update function updates the graphical state to reflect any changes to the data. It should be called anytime the state changes.

The drag function is deprecated and no longer works. However, you will notice it is still being called by some SVG elements. This was used early in development but never removed when replacements were developed. In a non-educational, shipping product any code referring to it should be removed. The reason it is still in the source is because it was handed in that way and this is meant to be an archive of what was graded.

The selectHandle function is the event handler for when a user clicks in one of the handles of a slider. It sets the global for the currently selected handle. The unselect function sets the state of all objects to not selected. It is similar to a “select none” action in most applications. It affects all sliders and buttons.

The buttonOver, buttonDown, buttonOut, and buttonUp functions are all event handlers working in conjunction with makeButtonDown and makeButtonUp to control the graphical state of the buttons. When the user mouse-downs on a button the global button is set to point to that button and the grical state of it changes. The user can then mouse-out and mouse-over the button with only the state of that button changing. If the user then mouse-ups anywhere outside of the button no action is performed and the global is set to null. This functionality is similar to native buttons on most platforms. If the user mouse-downs on a button and proceeds to mouse-up over the same button, it is considered a click. the click is reflected by changing the color data withing the boundaries of each attribute followed by the graphical update.

Color Picker

The next post (Part 2) will cover the application logic and associated code.