Vertical Color Gradient

From Director Online Wiki
Jump to: navigation, search

Description

Create a vertical, additive color gradient image

Code

-- VerticalGradientColor gradientSizeX, gradientSizeY, startRGB, endRGB
--   Example:
--   member("image").image = VerticalGradientColor(600,400,rgb(255,100,0),rgb(25,100,255))
 
on VerticalGradientColor gradientSizeX, gradientSizeY, startRGB, endRGB
  myGradientImage = image(gradientSizeX, gradientSizeY, 32,0)
  if startRGB = endRGB then
    myGradientImage.fill(myGradientImage.rect, startRGB)
    return myGradientImage
  else
    myGradientImage.fill(myGradientImage.rect,rgb(0,0,0))
  end if
  redDiff = endRGB.red - startRGB.red
  if redDiff = 0 then redDiff = 1
  if redDiff then
    absDiff = abs(redDiff)
    dir = redDiff/absDiff
    compGradient = image(1,absDiff,32,0)
    repeat with y = 0  to absDiff
      compGradient.fill(rect(0,y,1,y+1),rgb(startRGB.red+y*dir,0,0))
    end repeat
    myGradientImage.copyPixels(compGradient,myGradientImage.rect,compGradient.rect,[#ink:34])
  end if
  greenDiff = endRGB.green - startRGB.green
  if greenDiff = 0 then greenDiff = 1
  if greenDiff then
    absDiff = abs(greenDiff)
    dir = greenDiff/absDiff
    compGradient = image(1,absDiff,32,0)
    repeat with y = 0  to absDiff
      compGradient.fill(rect(0,y,1,y+1),rgb(0,startRGB.green+y*dir,0))
    end repeat
    myGradientImage.copyPixels(compGradient,myGradientImage.rect,compGradient.rect,[#ink:34])
  end if
  blueDiff = endRGB.blue - startRGB.blue
  if blueDiff = 0 then blueDiff = 1
  if blueDiff then
    absDiff = abs(blueDiff)
    dir = blueDiff/absDiff
    compGradient = image(1,absDiff,32,0)
    repeat with y = 0  to absDiff
      compGradient.fill(rect(0,y,1,y+1),rgb(0,0,startRGB.blue+y*dir))
    end repeat
    myGradientImage.copyPixels(compGradient,myGradientImage.rect,compGradient.rect,[#ink:34])
  end if
  return myGradientImage
end