Skip to content

colorized effect does not work in hexa, but fine in rgb #1070

Closed
@mrbbp

Description

@mrbbp

i was working on a 1bit sprite sheet sketch with color effect to tint() black pixel.
sorry i do not code my self, it's ia coded.

by filling the screen with sprites, some "hole" in the filling appearing.
i was looking for a beat to understand that the problem was coming from the way i define color for the custom blendMode.
in the example, nothing is show (all is white) in hexa color, and color is shown in rgb.
on my project the result is a more weird. i have some sprite displayed, and some white hole.

if think it's a bug
i can purpose my all script if this does not convince you (but it's the mess)

// Démonstration du bug de couleur avec remplissage complet d'écran
PImage spriteSheet;      // Feuille de sprites
PImage colorizedSprite;  // Sprite colorisé
int SPRITE_SIZE = 8;     // Taille d'un sprite
int DISPLAY_SIZE = 16;   // Taille d'affichage
boolean useHexadecimal = true;  // Mode de couleur

void setup() {
  size(400, 400);
  noSmooth();  // Désactiver l'anti-aliasing
  
  // Charger la spritesheet
  spriteSheet = loadImage("trame_90deg_5.png");  // Remplacez par votre fichier
  
  // Extraire et coloriser le sprite initial
  updateColorizedSprite();
}

void draw() {
  background(255);
  
  // Remplir l'écran avec le sprite colorisé
  for (int y = 0; y < height; y += DISPLAY_SIZE) {
    for (int x = 0; x < width; x += DISPLAY_SIZE) {
      image(colorizedSprite, x, y, DISPLAY_SIZE, DISPLAY_SIZE);
    }
  }
  
  // Afficher le mode actuel et la valeur de couleur
  fill(0);
  textSize(14);
  if (useHexadecimal) {
    text("Mode: HEXADECIMAL (0x88FF88)", 10, 20);
  } else {
    text("Mode: RGB (128, 255, 128)", 10, 20);
  }
  text("Appuyez sur ESPACE pour basculer", 10, 40);
}

// Mettre à jour le sprite selon le mode de couleur actuel
void updateColorizedSprite() {
  color spriteColor;
  
  if (useHexadecimal) {
    // Mode hexadécimal
    spriteColor = color(0x88FF88);  // Bleu foncé en hexadécimal
  } else {
    // Mode RGB
    spriteColor = color(128, 255, 128);  // Même bleu foncé en RGB
  }
  
  // Extraire et coloriser un sprite
  int index = 0;  // Premier sprite, vous pouvez changer ça
  
  // Calculer la position dans la spritesheet
  int cols = spriteSheet.width / SPRITE_SIZE;
  int col = index % cols;
  int row = index / cols;
  
  // Extraire le sprite original
  PImage originalSprite = spriteSheet.get(col * SPRITE_SIZE, row * SPRITE_SIZE, SPRITE_SIZE, SPRITE_SIZE);
  
  // Créer une nouvelle image pour le sprite colorisé
  colorizedSprite = createImage(SPRITE_SIZE, SPRITE_SIZE, ARGB);
  
  // Appliquer la couleur
  originalSprite.loadPixels();
  colorizedSprite.loadPixels();
  
  for (int i = 0; i < originalSprite.pixels.length; i++) {
    color c = originalSprite.pixels[i];
    
    // Si le pixel est noir ou foncé (motif)
    if (brightness(c) < 50) {
      colorizedSprite.pixels[i] = spriteColor;  // Appliquer la couleur souhaitée
    } else {
      colorizedSprite.pixels[i] = color(255);  // Pixel blanc
    }
  }
  
  colorizedSprite.updatePixels();
}

void keyPressed() {
  if (key == ' ') {
    // Basculer entre les modes
    useHexadecimal = !useHexadecimal;
    // Mettre à jour le sprite avec la nouvelle couleur
    updateColorizedSprite();
  }
}

on this image, there is only few hole in the blue area and more in the green area... but it is more hasardous
Image

MacOs 14.7.3
Processing 4.3.1 and 4.4.1
macbook pro 14" M1 2020

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions