#! /usr/bin/env python
# -*- coding:Shift_JIS -*-
from gimpfu import *

def isSelected(NorImg):
  b = False
  b, x1, y1, x2, y2 = pdb.gimp_selection_bounds(NorImg)
  return b

def python_SelectSpot(img, drawable, color, blur, alpha, isRelease):
  if not isSelected(img):
    pdb.gimp_message('There is no selection!')
    return
  # + 前処理 バックアップ
  img.undo_group_start()
  old_layer = img.active_layer
  old_color=gimp.get_foreground()
  pdb.gimp_context_set_foreground(color)	# 色を設定
  
  # + スポット処理
  layer = gimp.Layer(img, "Spot Light", drawable.width, drawable.height, RGB_IMAGE, alpha, MULTIPLY_MODE)
  img.add_layer(layer, 0)			# 黒レイヤーを０番に追加
  selection = pdb.gimp_selection_save(img)	# チャンネルに選択範囲を保存
  pdb.gimp_selection_none(img)			# 選択範囲を一旦解除して塗りつぶす
  pdb.gimp_edit_fill(layer, FOREGROUND_FILL)	# 
  pdb.gimp_selection_load(selection)		# 選択範囲の復帰
  
  pdb.gimp_image_set_active_layer(img,layer)	# 追加したレイヤーをアクティブにする
  pdb.gimp_selection_feather(img, blur)		# 選択範囲をぼかす
  pdb.gimp_edit_clear(layer)			# 選択範囲内の画像を削除
  
  if isRelease:
    pdb.gimp_selection_none(img)		# 選択範囲の解除
  
  # + 後処理 復帰
  pdb.gimp_context_set_foreground(old_color)
  pdb.gimp_image_set_active_layer(img,old_layer)
  img.undo_group_end()
  return


register(
        "python_fu_SelectSpot",
        "Select to Spot",
        "Select -> Spot Light !",
        "Keina<keines.2007@gmail.com>",
        "k.keina",
        "2008.11.21(Fri) 13:33",
        "<Image>/Filters/Misc/Select Spot...",
        "RGB*, GRAY*",
        [
            (PF_COLOR	, "color"	, "Color:"		, (0, 0, 0)),
            (PF_SPINNER	, "blur"	, "Blur:"		,  5, (0,100,1)),
            (PF_SLIDER	, "alpha"	, "Alpha:"		, 20, (0, 100, 10)),
            (PF_TOGGLE	, "isRelease"	, "release select:"	, TRUE)
        ],
        [],
        python_SelectSpot)

main()
