9
8

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.

【JavaScript+Photoshop】画像の白色を透過させる

Last updated at Posted at 2014-08-27

各チャンネルの色が乗った部分のみを抽出して乗算することで、白色を排除しています。
カラーモードは、RGB・CMYK・グレースケールに対応。

#target "Photoshop"


function CheckSelection()
{
	var select_bool = true;
	try
	{
		app.activeDocument.selection.bounds;
	}
	catch(e)
	{
		select_bool = false;
	}
	return select_bool;
}


try
{
	if (app.documents.length == 0){throw("ファイルが開かれていません。");}
	
	with(app.activeDocument)
	{
		var color_mode = mode;
		if ("DocumentMode.GRAYSCALE,DocumentMode.RGB,DocumentMode.CMYK".indexOf(color_mode.toString()) == -1)
		{
			throw("ファイルのカラーモードがRGB・CMYK・グレースケールのものしか処理できません。");
		}
		
		alert("処理実行します。途中でエラーが起きた場合は保存しないでください。");
		
		flatten();
		
		var doc_channels = channels;
		for (i=0; i<doc_channels.length; i++)
		{
			if (doc_channels[i].kind == ChannelType.COMPONENT)
			{
				selection.load(doc_channels[i]);
				selection.invert();
				
				var new_layer = artLayers.add();
				new_layer.name = "Finish";
				new_layer.blendMode = BlendMode.MULTIPLY;
								
				var channel_name = doc_channels[i].name;
				var solid_color = new SolidColor();
				if ("グレー,gray".indexOf(channel_name) != -1)
				{
					solid_color.gray.gray = 100;
				}
				else if ("レッド,red".indexOf(channel_name) != -1)
				{
					solid_color.rgb.red = 0;
					solid_color.rgb.green = 255;
					solid_color.rgb.blue = 255;
				}
				else if ("グリーン,green".indexOf(channel_name) != -1)
				{
					solid_color.rgb.red = 255;
					solid_color.rgb.green = 0;
					solid_color.rgb.blue = 255;
				}
				else if ("ブルー,blue".indexOf(channel_name) != -1)
				{
					solid_color.rgb.red = 255;
					solid_color.rgb.green = 255;
					solid_color.rgb.blue = 0;
				}
				else if ("シアン,cyan".indexOf(channel_name) != -1)
				{
					solid_color.cmyk.cyan = 100;
					solid_color.cmyk.magenta = 0;
					solid_color.cmyk.yellow = 0;
					solid_color.cmyk.black = 0;
				}
				else if ("マゼンタ,magenta".indexOf(channel_name) != -1)
				{
					solid_color.cmyk.cyan = 0;
					solid_color.cmyk.magenta = 100;
					solid_color.cmyk.yellow = 0;
					solid_color.cmyk.black = 0;
				}
				else if ("イエロー,yellow".indexOf(channel_name) != -1)
				{
					solid_color.cmyk.cyan = 0;
					solid_color.cmyk.magenta = 0;
					solid_color.cmyk.yellow = 100;
					solid_color.cmyk.black = 0;
				}
				else if ("ブラック,black".indexOf(channel_name) != -1)
				{
					solid_color.cmyk.cyan = 0;
					solid_color.cmyk.magenta = 0;
					solid_color.cmyk.yellow = 0;
					solid_color.cmyk.black = 100;
				}

				if (CheckSelection())
				{
					selection.fill(solid_color, ColorBlendMode.NORMAL , 100, false);
				}
			}
		}

		selection.deselect();

		var doc_layers = layers;
		for (j=0; j<doc_layers.length; j++)
		{
			if (doc_layers[j].isBackgroundLayer){doc_layers[j].remove();}
		}

		if (layers.length != 1)
		{
			mergeVisibleLayers();
			artLayers[0].name = "Finish"
		}
		else
		{
			artLayers[0].blendMode = BlendMode.NORMAL;
		}

		alert("処理完了しました。");
	}
}
catch(err_str)
{
	alert(err_str);
}
9
8
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
9
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?