Compare Colors in GameMaker

Compare Colors in GameMaker

Today we’ve got a very niche script to share with you! We actually released this back on the GameMaker Marketplace, but now that it’s ear-marked for closure we thought we’d immortalise it here on our own blog.

This script allows you to compare the visual similarity of two colors in code. Does your player need to mix potions to get the right color? Maybe your game features an animal who needs to hide in a place where they’re well camouflaged. Now you can easily test the visual similarity between two colors with this GML script.

It’s fairly straightforward, merely taking the difference between RGB values of each colour and comparing them to the maximum possible difference as seen below:

/// color_compare_simple( col1, col2 );
function color_compare_simple(argument0, argument1) {

	// RETURNS THE CLOSENESS OF TWO COLORS WITH A VALUE FROM 0-1 (0 = OPPOSITES, 1 = THE SAME COLOR);
	// A VALUE OF ~0.97 is around the threshold of a "Just Noticeable Difference".

	// SET UP RETURN VARIABLE
	var cqink_toReturn = -1;

	// BREAK UP COLORS INTO RGB
	var cqink_col1R = color_get_red(argument0);
	var cqink_col1G = color_get_green(argument0);
	var cqink_col1B = color_get_blue(argument0);
	var cqink_col2R = color_get_red(argument1);
	var cqink_col2G = color_get_green(argument1);
	var cqink_col2B = color_get_blue(argument1);

	// CALCULATE DIFFERENCE AS A % OF THE HIGHEST POSSIBLE DISTANCE
	var cqink_Rdist = abs(cqink_col1R-cqink_col2R);
	var cqink_Gdist = abs(cqink_col1G-cqink_col2G);
	var cqink_Bdist = abs(cqink_col1B-cqink_col2B);
	var cqink_currentDist = cqink_Rdist+cqink_Gdist+cqink_Bdist;
	cqink_toReturn = 1-(cqink_currentDist/765);


	return cqink_toReturn;


}

When you put two colors into the script, it will calculate the difference between them and return a value between 0 and 1. A value of 0 represents two entirely different colors, like pure black and pure white. A value of 1 represents two identical colors, for example if you pass the color constant c_lime into both arguments, the script would return 1.

Screenshots

To see the script in action, download the example project from the link below. We hope you enjoy finding a use for this extremely niche but kind of cool script!

Daniel

I make funky little games of all different kinds like Gyro Boss, Plunder Dungeons & Spellworm! Also making lots of fonts & free game assets.

Leave a Reply