2

When I call myIndexedSurface.get_palette(), it returns a list of 32-bit RGBA values. I figured that because there is an alpha value included, I might be able to set and use palette indices with transparency effects, too. These surfaces could then be blitted as overlays or masks more efficiently than Surfaces with full-depth (32-bit) per-pixel data.

So far, though, the palette element alpha values seem locked at 255 and won't change by any method I've tried:

  • .set_palette_at(idx, (r,g,b,A)) correctly sets r, g, and b, but leaves the alpha value at 255, regardless of A's value.

  • .set_palette(somePalette) raises ValueError: takes an alpha value of 255 if any somePalette RGBA tuple element has an alpha value other than 255.

Is what I'm trying to do possible? Is the alpha value in each palette-list element purely decorative, that it might be used more easily by other processes that expect 32-bit (RGBA) pixel data instead of just 24 (RGB)?

I feel like indexed surfaces in Pygame may not be equipped to handle this degree of complexity to begin with, but I also cannot think of or find a reason for why it shouldn't. Perhaps Pygame has one of its mysterious special_flags for this purpose?

I have tried initializing the Surface itself via myIndexedSurface = pygame.Surface(surfSize).convert(8) and myIndexedSurface = pygame.Surface(surfSize, flags=pygame.SRCALPHA).convert(8) to no effect.

I have not yet looked at PIL or Pygame's PixelArray for a solution.

Augusta
  • 7,171
  • 5
  • 24
  • 39
  • For those wondering, I've tried other methods to produce the overlay Surface I need, and have even found a couple. These methods are very slow, though, and involve manually altering the alpha values pixel-by-pixel. I've been working on a modified solution based on answers to [this question's answers](http://stackoverflow.com/questions/5940935/pygame-how-to-draw-in-non-rectangle-clipping-area) and [this other answer, specifically](http://stackoverflow.com/a/16930209/3408500). – Augusta Feb 15 '15 at 11:28
  • 1
    To answer your question why they shouldn't: indexed image formats came from a time where the numbers of colors displayable were severely limited (2, 4, 16, 32, 256). So making one of them an alpha-color for sprites made sense. Once you have the capability of displaying full 32-bit-RGBA (and the disk/ram-storage to accommodate this), there is no need for indexed images. So it's like asking why motorcycles don't come with training-wheels - in theory, they could. In practice, it makes no sense. – deets Feb 15 '15 at 17:04
  • @deets Hm, that is true. I guess there's not much point in this kind of (micro?)optimization if you're already in a processing environment where 8-bit opacity is a thing... I was thinking more of a technical limitation than a change in circumstance, though. – Augusta Feb 15 '15 at 17:07
  • not sure I understand what you mean by this (not a native speaker), but if you are asking why the interface of pygame doesn't support this as a convenience: I guess it enforces the conventions held up by the actual indexed file-formats which said surface might eventually end up being stored to - if it weren't, you'd get a deferred error-message instead. In the end what you want should be trivial & fast using Numpy-support in pygame. And yes, iterating through all pixels then happens, but I see no way around that anyways.API-hidden or not. – deets Feb 16 '15 at 13:20
  • @deets Hm. I thought it might turn out that way, or else there'd be support for something like `.convert_alpha(8)`, where there does not seem to be. Looks like I'll have to go with the pixel-by-pixel method, or some other godless solution like mashing some new blending method directly into `blit`. – Augusta Feb 17 '15 at 01:42
  • 1
    Trust me, coming from an age where 4-5 bitplanes sanely accessible only by 16 pixels at a time and a color-space of 4096 was gold, I rejoice over the level of per-pixel-freedom of nowadays ;) – deets Feb 17 '15 at 08:52

1 Answers1

1

As you had seen by yourself it does not. What you describe is kind of new invented approach. Four values are there just because it so declared in pyGame for color values, and some other properties. For example if you try this

S = pygame.Surface ((w,h),0,24)
r,g,b,a =S.get_masks()

You will see tuple of four values, despite 24 bit surface obviously has only three bytes to process, so the alpha just doesnt come in play by further blitting. If the data on your backend is originally indexed, like just number arrays, I would personally describe the combining logic also on the backend, using mask arrays.

Mikhail V
  • 1,416
  • 1
  • 14
  • 23