Skip navigation
newbie..
Currently Being Moderated

How to change the color of same coloured sections of an image according to the colorpicker..?

Sep 21, 2012 12:14 AM

Hi.. How r u all..?


I'm doing a small program to change the color of those sections of an image which are having the same color, according to the color selected from the colorpicker..


Say we have selected a color from the colorpicker, and then the mouseclick event on the image changes the color of those parts in the image which are having the same color as the clicked portion (or pixel) to the selected color.. If we've selected FF0000 from the colorpicker, then clicked on 3300CC coloured pixel in the image. This should change the color of all the 3300CC coloured pixels in the image..

 

I've a colorpicker [colpickr] and a movieclip [mc] of the image on the stage. I created a bitmap of that same same image (imag1) using code. Selecting the color from the colorpicker and a mouse click on the movieclip (here I used getPixel) will change that color in the bitmap to the selected color (with setPixel). So here I've two images; one is a movieclip (inorder to add listener for click event) and one is a bitmap (inorder to set the property setPixel). But I need only a single image.

Please help me regarding this.. Given below is my (horrible) code..

 

 

import flash.events.MouseEvent;

import flash.display.Bitmap;

import flash.display.BitmapData;

import flash.net.URLRequest;

import flash.display.Loader;

 

var c = 0;

var myBitmapData:BitmapData;

var bm:Bitmap;

 

colpickr.selectedColor = 0xffffff;

 

var preLoader;

var urlReq;

var preloader_img:MovieClip;

preLoader =new Loader();

preLoader.unloadAndStop();

urlReq = new URLRequest("imag1.jpg"); // i've selected the same image as that of movieclip mc

preLoader.load(urlReq);

 

preLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,preLoaderC omplete);

 

//creates a bitmap image of mc..

function preLoaderComplete(loadEvent:Event) {

    myBitmapData = new BitmapData(mc.width,mc.height);

    myBitmapData.draw(mc);

    bm = new Bitmap(myBitmapData);

    addChild(bm);

    bm.width = mc.width;

    bm.height = mc.height;

}

 

mc.addEventListener(MouseEvent.CLICK, _onMouseClick);

 

//gets the color of mc and sets the color to bitmap

function _onMouseClick(event:MouseEvent):void {

    var myColor:uint = myBitmapData.getPixel(mc.mouseX,mc.mouseY);

    c = myColor.toString(16);

    for (var i=0; i<bm.width; i++) {

        for (var j=0; j<bm.height; j++) {

            if (myBitmapData.getPixel(i,j).toString(16) == c) {

                myBitmapData.setPixel(i,j,colpickr.selectedColor);

            }

        }

    }

}

 
Replies
  • kglad
    62,196 posts
    Jul 21, 2002
    Currently Being Moderated
    Sep 21, 2012 7:09 AM   in reply to newbie..

    try this:

     

    import flash.events.MouseEvent;

    import flash.display.Bitmap;

    import flash.display.BitmapData;

    import flash.net.URLRequest;

    import flash.display.Loader;

     

    var c=0;

    var myBitmapData:BitmapData;

    var bm:Bitmap;

     

    colpickr.selectedColor=0xffffff;

     

    var preLoader;

    var urlReq;

    var preloader_img:MovieClip;

    preLoader =new Loader();

    preLoader.unloadAndStop();

    urlReq=new URLRequest("z_bitmaps/image1.jpg");// i've selected the same image as that of movieclip mc

    preLoader.load(urlReq);

     

    preLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,preLoaderC omplete);

     

    //creates a bitmap image of mc..;

    function preLoaderComplete(loadEvent:Event) {

        bm = Bitmap(loadEvent.target.loader.content);

        addChild(bm);

    }

    stage.addEventListener(MouseEvent.CLICK, f);

     

    function f(e:MouseEvent):void{

        if(bm.hitTestPoint(mouseX,mouseY) && !colpickr.hitTestPoint(mouseX,mouseY)){

            _onMouseClick(e);

        }

    }

     

    //gets the color of mc and sets the color to bitmap

    function _onMouseClick(event:MouseEvent):void {

        c=bm.bitmapData.getPixel(bm.mouseX,bm.mouseY);

        for (var i=0; i<bm.width; i++) {

            for (var j=0; j<bm.height; j++) {

                if (bm.bitmapData.getPixel(i,j)==c) {

                    bm.bitmapData.setPixel(i,j,colpickr.selectedColor);

                }

            }

        }

    }

     
    |
    Mark as:
  • kglad
    62,196 posts
    Jul 21, 2002
    Currently Being Moderated
    Sep 21, 2012 10:13 PM   in reply to newbie..

    yes, just create a clone() of the original bitmapdata to use to determine which pixels to change.

     
    |
    Mark as:
  • kglad
    62,196 posts
    Jul 21, 2002
    Currently Being Moderated
    Sep 22, 2012 6:19 AM   in reply to newbie..

    you're welcome.

     
    |
    Mark as:
  • kglad
    62,196 posts
    Jul 21, 2002
    Currently Being Moderated
    Sep 25, 2012 7:31 AM   in reply to newbie..

    create seperate bitmaps for objects that you want to treat as individual objects.

     

    so, if you have a bitmap of a room that contains a flower etc, you need to create seperate bitmaps of the flower etc.

     

    and, there should be no problem loading a bitmap.  just cast your loader's content as a bitmap:

     

    var loader:Loader=new Loader();

    loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeF);

    loader.load(new URLRequest("image.jpg"));

    var bmp:Bitmap;

     

    function f(e:Event):void{

    bmp=Bitamp(loader.content);

    }

     
    |
    Mark as:
  • kglad
    62,196 posts
    Jul 21, 2002
    Currently Being Moderated
    Sep 26, 2012 9:38 AM   in reply to newbie..

    there's no easy way to do that.  you would need to create an algorithm that checks pixels to recognize boundaries between objects.  i'm pretty sure that is going to be a difficult task.

     
    |
    Mark as:
  • kglad
    62,196 posts
    Jul 21, 2002
    Currently Being Moderated
    Sep 28, 2012 6:39 AM   in reply to newbie..

    you're welcome.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points