Jump to content


Photo

Replace a Specific Color (In-Game)


  • Please log in to reply
7 replies to this topic

#1 Chris_Devl

Chris_Devl

    The Next Gabe Newell

  • GMC Member
  • 321 posts
  • Version:GM8

Posted 26 May 2012 - 04:36 AM

I want to allow players to change the color of certain parts of a sprite. I know I could layer just that part of the sprite ontop of the original sprite, and just image_blend it, but does anyone know of an easier way (an extension perhaps) that will change every pixel of a specific color on a sprite to another color?
  • 0

#2 NukeTheCat

NukeTheCat

    GMC Member

  • GMC Member
  • 460 posts
  • Version:GM8.1

Posted 26 May 2012 - 03:31 PM

how 'bout using the draw_sprite_ext?

draw_sprite_ext(spr_object,-1,x,y,image_xscale,image_yscale,0,current_color,1)
//draw_sprite_ext(sprite,subimg,x,y,xscale,yscale,rot,color,alpha)
//curent_color is a variable that the player can adjust.

But to use this function you have to have PRO
  • 0

#3 Gamer3D

Gamer3D

    Human* me = this;

  • GMC Member
  • 1587 posts
  • Version:GM8.1

Posted 26 May 2012 - 04:26 PM

how 'bout using the draw_sprite_ext?

Combine that with a mask (white in the regions you want to color, clear elsewhere) to get the effect you want.
  • 0

#4 Chris_Devl

Chris_Devl

    The Next Gabe Newell

  • GMC Member
  • 321 posts
  • Version:GM8

Posted 26 May 2012 - 06:21 PM


how 'bout using the draw_sprite_ext?

Combine that with a mask (white in the regions you want to color, clear elsewhere) to get the effect you want.


Yeah that's what I was hoping to avoid though.. having to make a mask for every peice of armor in the game..
  • 0

#5 brac37

brac37

    GMC Member

  • GMC Member
  • 765 posts
  • Version:GM7

Posted 30 May 2012 - 12:36 AM



how 'bout using the draw_sprite_ext?

Combine that with a mask (white in the regions you want to color, clear elsewhere) to get the effect you want.


Yeah that's what I was hoping to avoid though.. having to make a mask for every peice of armor in the game..


You can do that automatically with the transparent color feature of backgrounds and sprites, but that feature is no longer supported in gm8, I guess. You can still do something with blending, though. But I do not know how you can transfer information from one color channel to another. If you cannot do that, then you need to reserve values for each color channel to be replaced by the team color. The easiest is to reserve zero for each color channel.

Then you use black as the team color when designing the sprites. You cannot use black any more anywhere else in the sprite, but the darkest gray will do too. Now if you draw the sprite onto a black surface with extended blend mode (bm_one, bm_one), and next draw the surface eight times onto itself with the same extended blend mode, then the surface is black where you need to replace the color and white everywhere else. That surface can be used to make the team color sprite as follows. First, make a second surface and use draw_clear_alpha (team_color,0) on it. Next, draw the first surface with blend mode bm_subtract and the sprite with extended blend mode (bm_one, bm_one) on the second surface. At last, make a new sprite of the surface.
  • 0

#6 Arsanthania

Arsanthania

    GMC Member

  • GMC Member
  • 39 posts
  • Version:GM:Studio

Posted 30 May 2012 - 06:48 PM

I want to allow players to change the color of certain parts of a sprite. I know I could layer just that part of the sprite ontop of the original sprite, and just image_blend it, but does anyone know of an easier way (an extension perhaps) that will change every pixel of a specific color on a sprite to another color?


Couldn't you just use a surface? I would assume you're using pro/standard or something, and if so, surfaces are usable-you can draw dots on a surface, and as long as you don't clear the surface, it will look as though it's just a sprite that can dynamically change-just clear the surface and redraw the sprite to reset it.
  • 0

#7 Sypran

Sypran

    GMC Member

  • GMC Member
  • 33 posts

Posted 30 May 2012 - 11:49 PM

I had a similar question once and someone gave me this bit of code... It has some problems, but it did it's job.
(it was called "sprite_replace_color")
/*
**  Usage:
**      sprite_replace_color(sprite,oldcolor,newcolor)
**
**  Arguments:
**      sprite      sprite to change
**      oldcolor    color that will be replaced
**      newcolor    color used as replacement
**
**  Returns:
**      (-1) on error
**
**  Notes:
**      This script replaces one color in a sprite with another.
**      No new sprites are created, the given sprite is changed.
**
**  GMLscripts.com
*/
{
    var sprite, oldc, newc;
    sprite = argument0;
    oldc  = argument1;
    newc  = argument2;

    var w, h, n, i, xo, yo, surf, tempsprite, newsprite, alphasprite;
    w  = sprite_get_width(sprite);
    h  = sprite_get_height(sprite);
    n  = sprite_get_number(sprite);
    xo = sprite_get_xoffset(sprite);
    yo = sprite_get_yoffset(sprite);

    surf = surface_create(w,h+1);
    surface_set_target(surf);

    for(i=0; i<n; i+=1) {
        draw_clear_alpha(c_black,1);
        draw_set_blend_mode_ext(bm_inv_dest_color,bm_one);
        draw_sprite(sprite,i,xo,yo);
        draw_set_blend_mode(bm_normal);
        draw_point_color(0,h,oldc);
        tempsprite = sprite_create_from_surface(surf,0,0,w,h+1,true,false,xo,yo);
        draw_clear_alpha(newc,1);
        draw_sprite(tempsprite,0,xo,yo);
        sprite_delete(tempsprite);
        if (i == 0) {
            newsprite = sprite_create_from_surface(surf,0,0,w,h,false,false,xo,yo);
            if (newsprite < 0) return -1;
        }else{
            sprite_add_from_surface(newsprite,surf,0,0,w,h,false,false);
        }
        draw_clear_alpha(c_white,1);
        draw_set_blend_mode_ext(bm_zero,bm_src_alpha);
        draw_sprite(sprite,i,xo,yo);
        if (i == 0) {
            alphasprite = sprite_create_from_surface(surf,0,0,w,h,false,false,xo,yo);
            if (alphasprite < 0) {
                sprite_delete(newsprite);
                return -1;
            }
        }else{
            sprite_add_from_surface(alphasprite,surf,0,0,w,h,false,false);
        }
        draw_set_blend_mode(bm_normal);
    }

    surface_reset_target();
    sprite_assign(sprite,newsprite);
    sprite_set_alpha_from_sprite(sprite,alphasprite);
    sprite_delete(newsprite);
    sprite_delete(alphasprite);
    surface_free(surf);
}


  • 0

#8 brac37

brac37

    GMC Member

  • GMC Member
  • 765 posts
  • Version:GM7

Posted 31 May 2012 - 03:15 PM

If the sprite_replace_color script works in gm8, then the transparent color feature is still supported in gm8. On the other hand, I am told that the blending scripts in the first link of my signature do not work in gm8, so there are incompatibilities.

EDIT: there are incompatibilities, but they have nothing to do with the transparent color feature, and the blending scripts in the first link of my signature have been converted to gm8.

Edited by brac37, 05 June 2012 - 06:50 PM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users