1from lazpaint import command, dialog, colors
2
3if __name__ == "__main__":
4  dialog.show_message("Library to access layer content.")
5
6DM_DRAW = "dmDrawWithTransparency"
7DM_LINEAR = "dmLinearBlend"
8DM_SET = "dmSet"
9DM_SET_EXCEPT_TRANSPARENT = "dmSetExceptTransparent"
10DM_XOR = "dmXor"
11
12BLEND_DRAW = 'Transparent'
13BLEND_LINEAR = 'LinearBlend'
14BLEND_LIGHTEN = 'Lighten'
15BLEND_SCREEN = 'Screen'
16BLEND_ADD = 'Additive'
17BLEND_LINEAR_ADD = 'LinearAdd'
18BLEND_COLOR_DODGE = 'ColorDodge'
19BLEND_DIVIDE = 'Divide'
20BLEND_NICE_GLOW = 'NiceGlow'
21BLEND_SOFT_LIGHT = 'SoftLight'
22BLEND_HARD_LIGHT = 'HardLight'
23BLEND_GLOW = 'Glow'
24BLEND_REFLECT = 'Reflect'
25BLEND_OVERLAY = 'Overlay'
26BLEND_DARK_OVERLAY = 'DarkOverlay'
27BLEND_DARKEN = 'Darken'
28BLEND_MULTIPLY = 'Multiply'
29BLEND_COLOR_BURN = 'ColorBurn'
30BLEND_DIFFERENCE = 'Difference'
31BLEND_LINEAR_DIFFERENCE = 'LinearDifference'
32BLEND_EXCLUSION = 'Exclusion'
33BLEND_LINEAR_EXCLUSION = 'LinearExclusion'
34BLEND_SUBTRACT = 'Subtract'
35BLEND_LINEAR_SUBTRACT = 'LinearSubtract'
36BLEND_SUBTRACT_INVERSE = 'SubtractInverse'
37BLEND_LINEAR_SUBTRACT_INVERSE = 'LinearSubtractInverse'
38BLEND_NEGATION = 'Negation'
39BLEND_LINEAR_NEGATION = 'LinearNegation'
40BLEND_XOR = 'Xor'
41BLEND_SVG_SOFT_LIGHT = 'SvgSoftLight'
42BLEND_MASK = 'Mask'
43BLEND_LINEAR_MULTIPLY_SATURATION = 'LinearMultiplySaturation'
44BLEND_LINEAR_HUE = 'LinearHue'
45BLEND_LINEAR_COLOR = 'LinearColor'
46BLEND_LINEAR_LIGHTNESS = 'LinearLightness'
47BLEND_LINEAR_SATURATION = 'LinearSaturation'
48BLEND_CORRECTED_HUE = 'CorrectedHue'
49BLEND_CORRECTED_COLOR = 'CorrectedColor'
50BLEND_CORRECTED_LIGHTNESS = 'CorrectedLightness'
51BLEND_CORRECTED_SATURATION = 'CorrectedSaturation'
52
53def get_id():
54  return command.send("LayerGetId?")
55
56def get_name() -> str:
57  return command.send("LayerGetName?")
58
59def get_opacity() -> int:
60  return command.send("LayerGetOpacity?")
61
62def get_blend_op():
63  return command.send("LayerGetBlendOp?")
64
65def get_visible() -> bool:
66  return command.send("LayerGetVisible?")
67
68def select_id(id):
69  command.send("LayerSelectId", Id=id)
70
71def set_name(name: str):
72  return command.send("LayerSetName", Name=str(name))
73
74def set_opacity(opacity: int):
75  return command.send("LayerSetOpacity", Opacity=opacity)
76
77def set_blend_op(blend_op):
78  return command.send("LayerSetBlendOp", BlendOp=blend_op)
79
80def set_visible(visible: bool):
81  return command.send("LayerSetVisible", Visible=visible)
82
83def new(name: str = None): #-> id
84  layer_id = command.send("LayerAddNew?")
85  if name is not None:
86    set_name(name)
87  return layer_id
88
89def paste_as_new(): #-> id
90  return command.send("EditPasteAsNewLayer?")
91
92def add_from_file(file_name): #-> [id]
93  return command.send("LayerFromFile?", FileName=file_name)
94
95def save_as(file_name:str, format:str = None): #-> str
96  return command.send("LayerSaveAs?", FileName=file_name, Format=format)
97
98def duplicate(): #-> id
99  return command.send("LayerDuplicate?")
100
101def merge_over():
102  command.send("LayerMergeOver")
103
104def is_empty() -> bool:
105  return command.send("IsLayerEmpty?")
106
107def is_transparent() -> bool:
108  return command.send("IsLayerTransparent?")
109
110def get_registry(identifier):
111  str_result = command.send("LayerGetRegistry?", Identifier=identifier)
112  if str_result == "":
113    return None
114  else:
115    return command.parse_str(str_result)
116
117def set_registry(identifier, value):
118  if value == None:
119    value = ""
120  elif isinstance(value, str):
121    value = repr(value)
122  command.send("LayerSetRegistry", Identifier=identifier, Value=value)
123
124def remove():
125  command.send("LayerRemoveCurrent")
126
127def get_count() -> int:
128  return command.send("GetLayerCount?")
129
130def rasterize():
131  command.send("LayerRasterize")
132
133def get_image_width(image) -> int:
134  return max([len(scanline) for scanline in image])
135
136def get_image_height(image) -> int:
137  return len(image)
138
139def get_image_size(image):
140  height = get_image_height(image)
141  if height == 0:
142    return (0,0)
143  else:
144    return (get_image_width(image), height)
145
146def put_image(x: int, y: int, image, mode=DM_DRAW, opacity=255):
147  width, height = get_image_size(image)
148  if width == 0 or height == 0: return
149  flattened = ""
150  for scanline in image:
151    flattened += "".join([str(color) for color in scanline]) + "00000000" * (width - len(scanline))
152  command.send("PutImage", X=x, Y=y, Width=width, Height=height, Data=flattened, Mode=mode, Opacity=opacity)
153
154def get_image(x: int, y: int, width: int, height: int):
155  flattened = command.send("GetImage?", X=x, Y=y, Width=width, Height=height)
156  str_pos = 0
157  image = []
158  for yb in range(0, height):
159    scanline = []
160    for xb in range(0, width):
161      scanline.append(colors.RGBA(int(flattened[str_pos:str_pos + 2],16), int(flattened[str_pos + 2:str_pos + 4],16), int(flattened[str_pos + 4:str_pos + 6],16), int(flattened[str_pos + 6:str_pos + 8],16)))
162      str_pos = str_pos + 8
163    image.append(scanline)
164  return image
165
166def get_pixel(x: int, y: int):
167  return colors.str_to_RGBA(command.send("GetPixel?", X=x, Y=y))
168
169def fill(color, mode=DM_DRAW):
170  command.send("LayerFill", Color=color, Mode=mode)
171
172def horizontal_flip():
173  command.send("LayerHorizontalFlip")
174
175def vertical_flip():
176  command.send("LayerVerticalFlip")
177
178def clear_alpha(back_color=None):
179  command.send("ImageClearAlpha", BackColor=back_color)
180
181def fill_background(back_color=None):
182  command.send("ImageFillBackground", BackColor=back_color)
183