# Handy PHP classes

I've just released my PHP Infusion Extension to enhance the functionality and the usability of the PHP scripting language. I'll offer you a few of my toys I've developed somewhen under the use of PHP Infusion to show you how you can simplify your code with a few new functions. I want to share 3 calculation classes at this time - take it as a afterwards christmas present ;)

## xColor - a color calculation class

**Attention:** This library isn't maintained anymore. Take a look at the jQuery color plugin instead!

I needed a bunch of functions to perform calculations on color values. I searched around the internet but I couldn't make a find, wherefore I've started with analysing and reverse engeneering color calclulations in graphic programs such as Fireworks and Adobe Photoshop. This step was really easy, most of the equations are really obvious. But I also needed a handy class which is able to handle several input formats like integers, HSL/HSV, RGB and so on. I decided to implement input and output filters and perform all calculations on RGB values. This concept worked really good. So the interface for the class looks like this:

<?php $xc = new xColor; $xc->setType(T_COLOR_HEX, T_COLOR_RGB); $c1 = $xc->random(); $c2 = $xc->random(); $xc->setType(T_COLOR_RGB, T_COLOR_HEX); echo $xc->red($c1); // 90FFFF echo $xc->green($c1); // FF0CFF echo $xc->blue($c1); // FFFFB8 echo $xc->webround($c2); // 66CC99 // Get the color point on a gradient $xc->gradientLevel($c1, $c2, 100, rand(0,100)); ?>

I believe "gradient level" is not a admitted term. Think about the gradient level as a point on a gradient between two colors. In the example above I used a step width of 100, so that the point you want to choose also have to be in the interval *[0, 100]*. 100 is a good example to represent percents as a color - for example to visualize a trend from 0% to 100% with a gradient from red to green.

## xFraction - a fraction calculation class

**Attention:** This library isn't maintained anymore. Take a look at the fraction.js library instead!

This class is the result of thinking about numbers - and yes a little part of my own wolframalpha. In general we could represent all rational numbers as a fraction. Trade this class as the implementation of this idea. The first thing I did was also finding equations to represent every number as a fraction. Natural numbers, integers, rational and real numbers. Even if I cancel the fractions (using an eclidean based *GCD()* implementation) after every real operation, there is the problem with the maximum size. PHP does not offer int64 before PHP6 (never ;) ), so we could trade this class more as a proof of concept because the least value is 1/(2^{31}-1) and the largest is (2^{31}-1)/1 because PHP does not have unsigned integers. But let's come to the example:

<?php $xf = new xFraction; $tmp = $xf->div($xf->mul("9.4'31'", [-4, 3]), 4.9); var_dump($xf->add($tmp, 23)); // [297371, 14553, 1] ~ 20,434 var_dump($xf->mul(-2, [22, 7])); // [44, 7, -1] ~ -2PI var_dump($xf->equal("7.44'5'", [6701, 900])); // true ?>

As you see, I've wrote a more intelligent input detection as with xColor - BTW if you wonder about the short form of arrays, take a look at my PHP Patches. You are able to pass strings, doubles, integers and arrays to every function. If you use string parameter, the input is trade as double value with the possebility to highlight a repeating part. I've started to implement an intelligent detection of repeating places with real doubles, but this is no fun. I wouldn't say, that I hate double, but I really don't like it's approximated inaccuracy. A little side note: The result of every function is an array with three elements; numerator, denominator and the sign. To get a double from this result you can use *$ret[0] * $ret[2] / $ret[1]*

## xPassport - a class to validate german passports

xPassport is the fledgling class of this collection. This class is one of my first classes from ~~too many~~ a few years ago. As the name suggests, it is possible to get some information from german passport numbers like the age, the date of issue and with the birth date we can check the age - for example "least 18". Did you know, that the scramble value is 7, 3, 1, 7, 3, ...? This is cool because we can easily use a binary calculation :-)

<?php $xp = new xPassport; if(false !== ($age = $xp->getAge('0123456789d<<0123456<0123456<<<<<<<0')) { echo 'You are ', $age, ' years old'; } else { echo 'The given number is not correct!'; } ?>

Okay, I hope you enjoy these classes. Happy playing and reading of the source :-)

You might also be interested in the following

**Sorry, comments are closed for this article. Contact me if you have an inventive contribution.**