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);
}
}
}
}
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);
}
}
}
}
hello sir.. thank you so much.. it's very helpful.. ![]()
But if we've 4 different colors in the image, say red, green, yellow & blue, and we changed red, green and yellow to blue (now there remains only blue), then the next change in color applies to the whole image.. Can it be done like the mouseclick changes the color according to the original image..?? Say, we changed red, green and yellow to blue, the next mouseclick on the original red portions changes only those portion's color without affecting the original green and yellow portions..
This is because I'm thinking of doing it like we have an image of a room with some paintings on the wall.. changing the wall color shoudln't change the color of the painting though they are of same color..
Thanks a lot once again..
If I want to change the color of a single object, is there a way other than by giving the appropriate pixels..??
Say, there is a flower in a room, and the color of the flower is changed determining its pixels..
We may use getPixel(x,y,color1) and setPixel(x,y,color2)... but if the flower and the wall are of same color, this will change both of their colors..
Also if the image is upoladed dynamically using code, in that case how can we determine the exact pixels of the objects the user wants to change color..??
Could you please help in this reagrd..?? ![]()
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);
}
so does that mean we'll have to create separate bitmaps for each of the object present in the image??
If it's a static image we can identify the different objects in it.. but I wish to have a program that allows user to dynamically select the image to be loaded.. so in that case how can we select the objects separately..?? I know i'm asking a lot of doubts..
sorry for the concern that I caused you..
North America
Europe, Middle East and Africa
Asia Pacific