Contents
Articles
Behaviors
Books
Director News
Director Web Sites
FAQ
Games
Mailing Lists
News Groups
Project Examples
Reviews
Software
Tools
Useful Web Sites
Utilities
Xtras

Don't miss these
How do I check if a user has the needed fonts on their machine?
Animate Image Sequence
Play Soundfile
myMailTo
Number of Days in a Month
RegistryReader
Making Multiple Animated Buttons
Prime Number Xtra
Move Window [ stage or MIAW ]
Card Shuffler
 

 

 

Behavior Blend Mode Algorithms - Imaging Lingo

Added on 8/23/2005

 

Compatibilities:
D8 D8_5 D9 Mac Parent PC Shockwave

This item has not yet been rated

Author: Chunick (website)

These blend modes function in the same way as Photoshop's Blend Modes ie. you can "blend" two images together. Currently, the images must be the same size, but it wouldn't be too hard to adapt it by using the rectangle1. Intersect(rectangle2) function if trying to emulate Photoshop. The modes I have converted over to lingo so far are: multiply screen darken lighten difference negation exclusion overlay hardlight softlight

-- Blend Mode Algorithms
-- 2005 by Josh Chunick (josh@chunick.com)
-- This code is free to use in commercial applications
-- or however you want. If you use this code you
-- must keep the comments, including this message,
-- intact. Feel free to add any changes or make
-- improvements.

-- Blend Mode code adapted from Jens Gruschel's code
-- http://www.pegtop.net/delphi/blendmodes/

-- multiplies the RGB components of the pixels individually
-- and returns the resulting pixel colour to be used
--result := (a*b) SHR 8;
on multiplyBlendMode (image1, image2)
  -- using the source image for the width and height
  -- you can add your own code here to determine how
  -- you want to handle this.
  theWidth = image1.width - 1
  theHeight = image1.height - 1
  theImage = image(theWidth + 1, theHeight + 1, 32)
  -- running through each pixel in theImage
  repeat with x = 0 to theWidth
    repeat with y = 0 to theHeight
      theColour1 = image1.getPixel(point(x,y))
      theColour2 = image2.getPixel(point(x,y))
      theRed = theColour1.red * theColour2.red / 255.0
      theGreen = theColour1.green * theColour2.green / 255.0
      theBlue = theColour1.blue * theColour2.blue / 255.0
      theNewColour = color(theRed, theGreen, theBlue)
      theImage.setPixel(point(x, y), theNewColour)
    end repeat
  end repeat
  return theImage
end

-- 255 - ((255-a) * (255-b) SHR 8);
on screenBlendMode (image1, image2)
  theStart = the milliseconds
  -- using the source image for the width and height
  -- you can add your own code here to determine how
  -- you want to handle this.
  theWidth = image1.width - 1
  theHeight = image1.height - 1
  theImage = image(theWidth + 1, theHeight + 1, 32)
  theDivisor = 255.0
  -- running through each pixel in theImage
  repeat with x = 0 to theWidth
    repeat with y = 0 to theHeight
      theColour1 = image1.getPixel(point(x,y))
      theColour2 = image2.getPixel(point(x,y))
      theRed = 255 - ((255 - theColour1.red) * (255 - theColour2.red) / theDivisor)
      theGreen = 255 - ((255 - theColour1.green) * (255 - theColour2.green) / theDivisor)
      theBlue = 255 - ((255 - theColour1.blue) * (255 - theColour2.blue) / theDivisor)
      theNewColour = color(theRed, theGreen, theBlue)
      theImage.setPixel(point(x, y), theNewColour)
    end repeat
  end repeat
  put the milliseconds - theStart
  return theImage
end

--if a < b then
--  result := a
--else
--  result := b;
on darkenBlendMode (image1, image2)
  theStart = the milliseconds
  -- using the source image for the width and height
  -- you can add your own code here to determine how
  -- you want to handle this.
  theWidth = image1.width - 1
  theHeight = image1.height - 1
  theImage = image(theWidth + 1, theHeight + 1, 32)
  -- running through each pixel in theImage
  repeat with x = 0 to theWidth
    repeat with y = 0 to theHeight
      theColour1 = image1.getPixel(point(x,y))
      theColour2 = image2.getPixel(point(x,y))
      theRed = theColour2.red
      if theColour1.red < theColour2.red then theRed = theColour1.red
      theGreen = theColour2.green
      if theColour1.green < theColour2.green then theGreen = theColour1.green
      theBlue = theColour2.blue
      if theColour1.blue < theColour2.blue then theBlue = theColour1.blue
      theNewColour = color(theRed, theGreen, theBlue)
      theImage.setPixel(point(x, y), theNewColour)
    end repeat
  end repeat
  put the milliseconds - theStart
  return theImage
end

--if a > b then
--  result := a
--else
--  result := b;
on lightenBlendMode (image1, image2)
  theStart = the milliseconds
  -- using the source image for the width and height
  -- you can add your own code here to determine how
  -- you want to handle this.
  theWidth = image1.width - 1
  theHeight = image1.height - 1
  theImage = image(theWidth + 1, theHeight + 1, 32)
  -- running through each pixel in theImage
  repeat with x = 0 to theWidth
    repeat with y = 0 to theHeight
      theColour1 = image1.getPixel(point(x,y))
      theColour2 = image2.getPixel(point(x,y))
      theRed = theColour2.red
      if theColour1.red > theColour2.red then theRed = theColour1.red
      theGreen = theColour2.green
      if theColour1.green > theColour2.green then theGreen = theColour1.green
      theBlue = theColour2.blue
      if theColour1.blue > theColour2.blue then theBlue = theColour1.blue
      theNewColour = color(theRed, theGreen, theBlue)
      theImage.setPixel(point(x, y), theNewColour)
    end repeat
  end repeat
  put the milliseconds - theStart
  return theImage
end

--result := abs(a-b);
on differenceBlendMode (image1, image2)
  theStart = the milliseconds
  -- using the source image for the width and height
  -- you can add your own code here to determine how
  -- you want to handle this.
  theWidth = image1.width - 1
  theHeight = image1.height - 1
  theImage = image(theWidth + 1, theHeight + 1, 32)
  -- running through each pixel in theImage
  repeat with x = 0 to theWidth
    repeat with y = 0 to theHeight
      theColour1 = image1.getPixel(point(x,y))
      theColour2 = image2.getPixel(point(x,y))
      theRed = abs(theColour1.red - theColour2.red)
      theGreen = abs(theColour1.green - theColour2.green)
      theBlue = abs(theColour1.blue - theColour2.blue)
      theNewColour = color(theRed, theGreen, theBlue)
      theImage.setPixel(point(x, y), theNewColour)
    end repeat
  end repeat
  put the milliseconds - theStart
  return theImage
end

-- the true "opposite" of Difference Blend Mode
--result := 255 - abs(255-a-b);
on negationBlendMode (image1, image2)
  theStart = the milliseconds
  -- using the source image for the width and height
  -- you can add your own code here to determine how
  -- you want to handle this.
  theWidth = image1.width - 1
  theHeight = image1.height - 1
  theImage = image(theWidth + 1, theHeight + 1, 32)
  -- running through each pixel in theImage
  repeat with x = 0 to theWidth
    repeat with y = 0 to theHeight
      theColour1 = image1.getPixel(point(x,y))
      theColour2 = image2.getPixel(point(x,y))
      theRed = 255 - abs(255 - theColour1.red - theColour2.red)
      theGreen = 255 - abs(255 - theColour1.green - theColour2.green)
      theBlue = 255 - abs(255 - theColour1.blue - theColour2.blue)
      theNewColour = color(theRed, theGreen, theBlue)
      theImage.setPixel(point(x, y), theNewColour)
    end repeat
  end repeat
  put the milliseconds - theStart
  return theImage
end

-- result := a + b - (a*b SHR 7);
on exclusionBlendMode (image1, image2)
  theStart = the milliseconds
  -- using the source image for the width and height
  -- you can add your own code here to determine how
  -- you want to handle this.
  theWidth = image1.width - 1
  theHeight = image1.height - 1
  theImage = image(theWidth + 1, theHeight + 1, 32)
  theDivisor = 127.0
  -- running through each pixel in theImage
  repeat with x = 0 to theWidth
    repeat with y = 0 to theHeight
      theColour1 = image1.getPixel(point(x,y))
      theColour2 = image2.getPixel(point(x,y))
      theRed = theColour1.red + theColour2.red - ((theColour1.red * theColour2.red) / theDivisor)
      theGreen = theColour1.green + theColour2.green - ((theColour1.green * theColour2.green) / theDivisor)
      theBlue = theColour1.blue + theColour2.blue - ((theColour1.blue * theColour2.blue) / theDivisor)
      theNewColour = color(theRed, theGreen, theBlue)
      theImage.setPixel(point(x, y), theNewColour)
    end repeat
  end repeat
  put the milliseconds - theStart
  return theImage
end

--if a < 128 then
--  result := (a*b) SHR 7
--else
--  result := 255 - ((255-a) * (255-b) SHR 7);
-- ****** this is hard light mode in photoshop ******
on overlayBlendMode (image1, image2)
  theStart = the milliseconds
  -- using the source image for the width and height
  -- you can add your own code here to determine how
  -- you want to handle this.
  theWidth = image1.width - 1
  theHeight = image1.height - 1
  theImage = image(theWidth + 1, theHeight + 1, 32)
  theDivisor = 127.0
  -- running through each pixel in theImage
  repeat with x = 0 to theWidth
    repeat with y = 0 to theHeight
      theColour1 = image1.getPixel(point(x,y))
      theColour2 = image2.getPixel(point(x,y))
      if  theColour1.red < 128 then
        theRed = theColour1.red * theColour2.red / theDivisor
      else
        theRed = 255 - ((255 - theColour1.red) * (255 - theColour2.red) / theDivisor)
      end if
      if  theColour1.green < 128 then
        theGreen = theColour1.green * theColour2.green / theDivisor
      else
        theGreen = 255 - ((255 - theColour1.green) * (255 - theColour2.green) / theDivisor)
      end if
      if  theColour1.blue < 128 then
        theBlue = theColour1.blue * theColour2.blue / theDivisor
      else
        theBlue = 255 - ((255 - theColour1.blue) * (255 - theColour2.blue) / theDivisor)
      end if
      theNewColour = color(theRed, theGreen, theBlue)
      theImage.setPixel(point(x, y), theNewColour)
    end repeat
  end repeat
  put the milliseconds - theStart
  return theImage
end

--if b < 128 then
--  result := (a*b) SHR 7
--else
--  result := 255 - ((255-b) * (255-a) SHR 7);
-- ******* this is overlay mode in photoshop *******
on hardlightBlendMode (image1, image2)
  theStart = the milliseconds
  -- using the source image for the width and height
  -- you can add your own code here to determine how
  -- you want to handle this.
  theWidth = image1.width - 1
  theHeight = image1.height - 1
  theImage = image(theWidth + 1, theHeight + 1, 32)
  theDivisor = 127.0
  -- running through each pixel in theImage
  repeat with x = 0 to theWidth
    repeat with y = 0 to theHeight
      theColour1 = image1.getPixel(point(x,y))
      theColour2 = image2.getPixel(point(x,y))
      if  theColour2.red < 128 then
        theRed = (theColour1.red * theColour2.red) / theDivisor
      else
        theRed = 255 - ((255 - theColour2.red) * (255 - theColour1.red) / theDivisor)
      end if
      if  theColour2.green < 128 then
        theGreen = (theColour1.green * theColour2.green) / theDivisor
      else
        theGreen = 255 - ((255 - theColour2.green) * (255 - theColour1.green) / theDivisor)
      end if
      if  theColour2.blue < 128 then
        theBlue = (theColour1.blue * theColour2.blue) / theDivisor
      else
        theBlue = 255 - ((255 - theColour2.blue) * (255 - theColour1.blue) / theDivisor)
      end if
      theNewColour = color(theRed, theGreen, theBlue)
      theImage.setPixel(point(x, y), theNewColour)
    end repeat
  end repeat
  put the milliseconds - theStart
  return theImage
end

--c := a * b SHR 8;
--result := c + a * (255 - ((255-a)*(255-b) SHR 8)-c) SHR 8;
-- ******** XFader soft light mode - better than photoshop's version *******
on softlightBlendMode (image1, image2)
  theStart = the milliseconds
  -- using the source image for the width and height
  -- you can add your own code here to determine how
  -- you want to handle this.
  theWidth = image1.width - 1
  theHeight = image1.height - 1
  theImage = image(theWidth + 1, theHeight + 1, 32)
  theDivisor = 255.0
  -- running through each pixel in theImage
  repeat with x = 0 to theWidth
    repeat with y = 0 to theHeight
      theColour1 = image1.getPixel(point(x,y))
      theColour2 = image2.getPixel(point(x,y))
      
      theComp = theColour1.red * theColour2.red / theDivisor
      theRed = theComp + theColour1.red * (255 - ((255 - theColour1.red) * (255 - theColour2.red) / theDivisor) - theComp) / theDivisor
      
      theComp = theColour1.green * theColour2.green / theDivisor
      theGreen = theComp + theColour1.green * (255 - ((255 - theColour1.green) * (255 - theColour2.green) / theDivisor) - theComp) / theDivisor
      
      theComp = theColour1.blue * theColour2.blue / theDivisor
      theBlue = theComp + theColour1.blue  * (255 - ((255 - theColour1.blue ) * (255 - theColour2.blue ) / theDivisor) - theComp) / theDivisor
      theNewColour = color(theRed, theGreen, theBlue)
      theImage.setPixel(point(x, y), theNewColour)
    end repeat
  end repeat
  put the milliseconds - theStart
  return theImage
end

 


Contact

MMI
36 South Court Sq
Suite 300
Newnan, GA 30263
USA

Send e-mail