1
\$\begingroup\$

In pygame, what does Surface.set_colorkey() do? Explain it in a very easy way please. Also, what exactly does it do? I heard it makes parts that are the same color as the colorkey transparent, but what is the point of that?

\$\endgroup\$
1
  • \$\begingroup\$ "what is the point of that?" It allows you to have transparency with a bitmap image. The transparency offered is very similar to a .gif image, where a pixel is either completely transparent, or completely opaque, never 'semi-opaque'. For instance, if you set the parts of the image that should be transparent to a fuchsia colour (#FF00FF), and you set the colorKey to that colour, it should show as transparent in your game. \$\endgroup\$ – Vaillancourt Sep 26 '16 at 20:17
2
\$\begingroup\$

It sets the color value that considered transparent when doing color-key transparency.

Color-key transparency is a technique that causes pixels matching the key color to be ignored (and thus become transparent) when rendering an image. It's a technique that was used a lot more frequently in the past, when we would still do CPU-side blitting of 2D images to the screen.

It was often used to make sprites look like they were sitting on the background images, instead of surrounded by a rectangle of dead, hot-pink pixels. Indeed, this is generally the point of any sort of transparency technique: to hide pixels you don't want to see, or to make them partially visible to achieve a certain effect.

Color-key transparency only supported fully-visible or fully-hidden pixels. These days it is largely superseded by alpha compositing, which uses a fourth dedicated channel in addition to the red, green and blue color channels to hold opacity information and blends the final pixels together based on the that information.

Color keying is fairly simple and required simpler assets. It can still be done fairly quickly, especially on modern CPUs, and so still survives in some APIs.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.