Compositions¶
When we are drawing , what we really do is to change the destination pixels color. Instead of just using (copy) the source color, the color be decided by both the source and the destination’s (old) color. This is where the composition modes come in.
Easygraphics (and the underlying Qt system) support three types of compositions:
- Bit Blit ( Raster Operation (ROP) / Bitwise Operation ): the source color and the destination color are combined using a boolean function. Both the source color and the destination color must be opaque.
- Alpha compositing : is the process of combining the source with the destination to create the appearance of partial or full transparency.
- Blend : is used in digital image editing and computer graphics to determine how two layers are blended into each other.
These three types is not strictly orthogonal, and some compositions have different names in each type.
Bit Blit (Raster Operations)¶
Bit Blit is the classic composition operation used in old graphic systems. Because it requires both the source color and the destination color to be opaque, it’s of limited use in modern graphics systems.
A classic usage of the raster operation is using bitwise XOR to draw and clear a shape, so as to create an animation.
The following example use bitwise XOR to draw and clear the ellipse. Note that other colors will not do the job.
from easygraphics import *
def main():
init_graph(400, 200)
set_render_mode(RenderMode.RENDER_MANUAL)
set_background_color("white")
x = 100
ellipse(x, 100, 100, 50) # draw a ellipse
set_color("white")
set_composition_mode(CompositionMode.SRC_XOR_DEST)
while is_run():
old_x = x
x = (x + 5) % 400
ellipse(old_x, 100, 100, 50) # clear the ellipse last drawn
ellipse(x, 100, 100, 50) # draw a new ellipse
delay_fps(30)
close_graph()
easy_run(main)
Alpha Compositing¶
Alpha Compositing is the mostly used composition type.
The following table shows the result of the alpha compositings.
Source and Source Over is the mostly used compistings.
Source and Destination are opaque | |||
Source | Source Over | source in | |
source out | source atop | xor | |
Destination | Destination Over | Destination in | |
Destination out | Destination atop | xor | |
Source and Destination are partially transparent | |||
Source | Source Over | source in | |
source out | source atop | xor | |
Destination | Destination Over | Destination in | |
Destination out | Destination atop | xor |