3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

色の違いを数値で求める

Posted at

N次元の距離を求めるのと同じであり、色をN次元化すればよい。色をRGBとして扱う場合、R、G、Bのそれぞれの成分を次元として扱う。

package
{
	import flash.display.Sprite;

	public class Test extends Sprite
	{

		public static function distance(rgb1:uint, rgb2:uint):Number
		{
			var r1:uint = (rgb1 & 0xff0000) >> 16;
			var g1:uint = (rgb1 & 0xff00) >> 8;
			var b1:uint = rgb1 & 0xff;

			var r2:uint = (rgb2 & 0xff0000) >> 16;
			var g2:uint = (rgb2 & 0xff00) >> 8;
			var b2:uint = rgb2 & 0xff;

			return Math.sqrt((r2 - r1) * (r2 - r1) + (g2 - g1) * (g2 - g1) + (b2 - b1) * (b2 - b1));
		}
	}
}

真っ黒(#000000)と真っ白(#ffffff)の差は、Math.sqrt(255255 + 255255 + 255255) => 約411となる。
真っ青(#0000ff)と真っ赤(#ff0000)の差は、Math.sqrt(255
255 + 0 * 0 + -255 * -255) => 約360となる。

RGBを一旦HSVに変換した上で明度差、彩度差、色相差で距離を求めた方が人間の間隔に近いかもしれない。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?