<commit_before><?php

namespace donatj\AlikeColorFinder;

class AlikeColorFinder {
	/**
	 * @param $subject
	 * @param $tolerance
	 */
	public function ExtractColorInfo( $subject, $tolerance ) {
		preg_match_all('/(?P<hex>#[0-9a-f]{3}(?:[0-9a-f]{3})?)|(?:(?P<func>(?:rgb|hsl)a?)\s*\((?P<params>[\s0-9.%,]+)\))/i', $subject, $results, PREG_SET_ORDER);

		$colors = $this->extractColors($results);

		$colorStack = $colors;
		while( count($colorStack) > 1 ) {
			$colorOne = array_pop($colorStack);

			foreach( $colorStack as $colorTwo ) {
				$diff = $this->rgbaAbsDiff($colorOne['rgba'], $colorTwo['rgba']);

				if( $diff < $tolerance ) {
					$oneString = "rgba({$colorOne['rgba']['r']},{$colorOne['rgba']['g']},{$colorOne['rgba']['b']},{$colorOne['rgba']['a']})";
					$twoString = "rgba({$colorTwo['rgba']['r']},{$colorTwo['rgba']['g']},{$colorTwo['rgba']['b']},{$colorTwo['rgba']['a']})";

					$oneString = "({$colorOne['count']}) {$oneString}";
					$twoString = "({$colorTwo['count']}) {$twoString}";

					if( $colorOne['count'] > $colorTwo['count'] ) {
						$oneString = "*" . $oneString;
					} elseif( $colorOne['count'] > $colorTwo['count'] ) {
						$twoString = "* " . $twoString;
					}

					echo sprintf(" %30s %30s   diff: %d\n", $oneString, $twoString, $diff);

					$oneOrig = array_values($colorOne['orig']);
					$twoOrig = array_values($colorTwo['orig']);

					$max = max(count($oneOrig), count($twoOrig));

					for( $i = 0; $i < $max; $i++ ) {
						$oneString = !empty($oneOrig[$i]) ? $oneOrig[$i] : "";
						$twoString = !empty($twoOrig[$i]) ? $twoOrig[$i] : "";
						echo sprintf(" %30s %30s\n", $oneString, $twoString);
					}

					echo "\n";
				}
			}
		}
	}

	/**
	 * @param $results
	 * @return array
	 */
	private function extractColors( $results ) {
		$colors = [ ];
		foreach( $results as $result ) {
			$rgba = false;
			if( !empty($result['hex']) ) {
				$rgba = $this->hex2rgba($result['hex']);

			} else {
				switch( $result['func'] ) {
					case 'rgba':
						$params = array_map('\trim', explode(',', $result['params']));
						if( count($params) != 4 ) {
							echo "Invalid param count\n";
							continue;
						}

						$rgba = [ ];
						list($rgba['r'], $rgba['g'], $rgba['b'], $rgba['a']) = $params;
						break;
					case 'rgb':
						$params = array_map('\trim', explode(',', $result['params']));
						if( count($params) != 3 ) {
							echo "Invalid param count\n";
							continue;
						}

						$rgba = [ 'a' => 1 ];
						list($rgba['r'], $rgba['g'], $rgba['b']) = $params;
						break;<commit_msg>Color matching moved into extractColors<commit_after><?php

namespace donatj\AlikeColorFinder;

class AlikeColorFinder {
	/**
	 * @param $subject
	 * @param $tolerance
	 */
	public function ExtractColorInfo( $subject, $tolerance ) {
		$colors = $this->extractColors($subject);

		$colorStack = $colors;
		while( count($colorStack) > 1 ) {
			$colorOne = array_pop($colorStack);

			foreach( $colorStack as $colorTwo ) {
				$diff = $this->rgbaAbsDiff($colorOne['rgba'], $colorTwo['rgba']);

				if( $diff < $tolerance ) {
					$oneString = "rgba({$colorOne['rgba']['r']},{$colorOne['rgba']['g']},{$colorOne['rgba']['b']},{$colorOne['rgba']['a']})";
					$twoString = "rgba({$colorTwo['rgba']['r']},{$colorTwo['rgba']['g']},{$colorTwo['rgba']['b']},{$colorTwo['rgba']['a']})";

					$oneString = "({$colorOne['count']}) {$oneString}";
					$twoString = "({$colorTwo['count']}) {$twoString}";

					if( $colorOne['count'] > $colorTwo['count'] ) {
						$oneString = "*" . $oneString;
					} elseif( $colorOne['count'] > $colorTwo['count'] ) {
						$twoString = "* " . $twoString;
					}

					echo sprintf(" %30s %30s   diff: %d\n", $oneString, $twoString, $diff);

					$oneOrig = array_values($colorOne['orig']);
					$twoOrig = array_values($colorTwo['orig']);

					$max = max(count($oneOrig), count($twoOrig));

					for( $i = 0; $i < $max; $i++ ) {
						$oneString = !empty($oneOrig[$i]) ? $oneOrig[$i] : "";
						$twoString = !empty($twoOrig[$i]) ? $twoOrig[$i] : "";
						echo sprintf(" %30s %30s\n", $oneString, $twoString);
					}

					echo "\n";
				}
			}
		}
	}

	/**
	 * @param string $subject
	 * @return array
	 */
	private function extractColors( $subject ) {
		preg_match_all('/(?P<hex>#[0-9a-f]{3}(?:[0-9a-f]{3})?)|(?:(?P<func>(?:rgb|hsl)a?)\s*\((?P<params>[\s0-9.%,]+)\))/i', $subject, $results, PREG_SET_ORDER);

		$colors = [ ];
		foreach( $results as $result ) {
			$rgba = false;
			if( !empty($result['hex']) ) {
				$rgba = $this->hex2rgba($result['hex']);

			} else {
				switch( $result['func'] ) {
					case 'rgba':
						$params = array_map('\trim', explode(',', $result['params']));
						if( count($params) != 4 ) {
							echo "Invalid param count\n";
							continue;
						}

						$rgba = [ ];
						list($rgba['r'], $rgba['g'], $rgba['b'], $rgba['a']) = $params;
						break;
					case 'rgb':
						$params = array_map('\trim', explode(',', $result['params']));
						if( count($params) != 3 ) {
							echo "Invalid param count\n";
							continue;
						}

						$rgba = [ 'a' => 1 ];
						list($rgba['r'], $rgba['g'], $rgba['b']) = $params;
						break;
{'fraction_non_alphanumeric': 0.2548905674995158, 'fraction_numerical': 0.00852217702885919, 'mean_word_length': 3.4288164665523158, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1274820', 'n_tokens_mistral': 2393, 'n_tokens_neox': 1934, 'n_words': 320}