Blog

eXorithm – Execute Algorithm: View / Run Algorithm blend_colors

function blend_colors ($color1$color2$weight
{
  if ($weight<0 || $weight>1)
    throw new Exception"Weight must be between 0 and 1");
  
  $r1 = hexdecsubstr$color1, 0, 2));
  $g1 = hexdecsubstr$color1, 2, 2));
  $b1 = hexdecsubstr$color1, 4, 2));
  
  $r2 = hexdecsubstr$color2, 0, 2));
  $g2 = hexdecsubstr$color2, 2, 2));
  $b2 = hexdecsubstr$color2, 4, 2));
  
  $r = dechexround$r1$weight + $r2*(1-$weight)));
  $g = dechexround$g1$weight + $g2*(1-$weight)));
  $b = dechexround$b1$weight + $b2*(1-$weight)));
  
  if (strlen$r)==1) $r = '0'$r
  if (strlen$g)==1) $g = '0'$g
  if (strlen$b)==1) $b = '0'$b
  
  return $r$g$b

eXorithm – Execute Algorithm: Embed Algorithm sum_list


Embed This Algorithm

This page will help you embed the algorithm sum_list on a page on your own website. Just configure the inputs, then click the generate button to get a snippet of code you can paste onto your site. You have two options.

  1. You can embed the entire form. Users will be able to enter their own arguments, and will need to press the run button to execute the algorithm.
  2. You can add only the output of the algorithm to your website. There will be no argument inputs or run button.
numbers

Embed the form Embed only the output

eXorithm – Execute Algorithm: View / Run Algorithm watermark_image

function watermark_image ($image, $watermark, $position, $margin, $transparency
{
  $w = imagesx$image);
  $h = imagesy$image);
  
  $ww = imagesx$watermark);
  $hw = imagesy$watermark);
  
  $image2 = image_create_alpha$w, $h);
  imagecopy$image2, $image, 0, 0, 0, 0, $w, $h);
  
  imagealphablending$image2, true);
  imagesavealpha$image2, true);
  
  switch ($position) {
    case 'tl'
      $top = $margin
      $left = $margin
      break
    case 'tr'
      $top = $margin
      $left = $w$ww$margin
      break
    case 'br'
      $top = $h$hw$margin
      $left = $w$ww$margin
      break
    case 'bl'
      $top = $h$hw$margin
      $left = $margin
      break
    default
      $top = ($h$hw)/2;
      $left = ($w$ww)/2;
  }
  
  if ($transparency>0)
    imagecopy$image2, add_transparency$watermark, $transparency), $left, $top, 0, 0, $ww, $hw); 
  else
    imagecopy$image2, $watermark, $left, $top, 0, 0, $ww, $hw);
  
  return $image2
} 

eXorithm – Execute Algorithm: Embed Algorithm unique_image


Embed This Algorithm

This page will help you embed the algorithm unique_image on a page on your own website. Just configure the inputs, then click the generate button to get a snippet of code you can paste onto your site. You have two options.

  1. You can embed the entire form. Users will be able to enter their own arguments, and will need to press the run button to execute the algorithm.
  2. You can add only the output of the algorithm to your website. There will be no argument inputs or run button.
string
Embed the form Embed only the output

eXorithm – Execute Algorithm: View / Run Algorithm generate_graph_color

function generate_graph_color ($number
{
  $nums = array(2,6,18,8,20,24,5,15,1,22,19,21,11,3,9,4,10,12,14,16,7,17,23,25,13,0,26);
  
  $color = $nums$number % count$nums)];
  $color = base_convert$color, 10, 3);
  
  $r = floor$color / 100);
  $g = floor(($color % 100) / 10);
  $b = ($color % 10);
  
  $r = dechex$r*127);
  $g = dechex$g*127);
  $b = dechex$b*127);
  
  if (strlen$r)==1) $r = '0'$r
  if (strlen$g)==1) $g = '0'$g
  if (strlen$b)==1) $b = '0'$b
    
  return $r$g$b

Sort Multi Array

<?php

/**
 * sort_multi_array
 *
 * Sort a two-dimensional array by one (or more) of the elements in the nested arrays. Accepts a variable number of arguments.
 *
 * @version 0.1
 * @author Contributors at eXorithm
 * @link /algorithm/view/sort_multi_array Listing at eXorithm
 * @link /algorithm/history/sort_multi_array History at eXorithm
 * @license /home/show/license
 *
 * @param array $array Two dimensional array. Each array in the array should have the same keys.
 * @param mixed $key Key in the sub-arrays to sort by.
 * @return array
 */
function sort_multi_array($array=array(0=>array('surname'=>'Smith','givenname'=>'Henrietta'),1=>array('surname'=>'Smythe','givenname'=>'Stuart'),2=>array('surname'=>'Black','givenname'=>'Conrad'),3=>array('surname'=>'Smith','givenname'=>'Abigail'),4=>array('surname'=>'Eaves','givenname'=>'Ruth'),5=>array('surname'=>'Travis','givenname'=>'Earl')),$key='surname')
{
	$keys = array();
	for ($i=1;$i<func_num_args();$i++) {
		$keys[$i-1] = func_get_arg($i);
	}
	
	// create a custom search function to pass to usort
	$func = function ($a, $b) use ($keys) {
		for ($i=0;$i<count($keys);$i++) {
			if ($a[$keys[$i]] != $b[$keys[$i]]) {
				return ($a[$keys[$i]] < $b[$keys[$i]]) ? -1 : 1;
			}
		}
		return 0;
	};
	
	usort($array, $func);
	
	return $array;
}

?>

eXorithm – Execute Algorithm: View / Run Algorithm namePrefix

function namePrefix ($number
{
  //Similar scrip in jquery https://jsfiddle.net/ayn_/z2677vs5/
  
  $currentNum = $number
  
  $currentNum = preg_replace'{/s|,|$|h}'''$currentNum);
  
  
  if ($currentNum > 999 && $currentNum < 999999) {
    $newNum = ($currentNum / 1000);
    $newNum = floor$newNum*100)/100 . 'k'
  } else if ($currentNum > 999999) {
    $newNum = ($currentNum / 1000000);
    $newNum = floor$newNum*100)/100  . 'M'
  } else {
    $newNum = 'Error, number format is invalid'
  }
  
  
  return $newNum

Hailstone

<?php

/**
 * hailstone
 *
 * Calculates a hailstone sequence.
http://en.wikipedia.org/wiki/Collatz_conjecture
 *
 * @version 0.3
 * @author Contributors at eXorithm
 * @link /algorithm/view/hailstone Listing at eXorithm
 * @link /algorithm/history/hailstone History at eXorithm
 * @license /home/show/license
 *
 * @param number $number number to start with
 * @return array
 */
function hailstone($number=17)
{
	$result = array();
	
	while ($number > 1) {
		$result[] = $number;
		if ($number & 1)
			$number = 3 * $number + 1;
		else
			$number = $number / 2;
	}
	
	$result[] = $number;
	
	return $result;
}

?>