Create a disabled version of an image

After a correction on my greyscale formula in my last post from Mark on facebook I reviewed the results of my code and decided the disabled look wasn’t quite right. What I wanted was a a washed out looking version. This code and plugin modifies the alphaData of the image to 33% of the original and greys the imageData 15% more. You can modify the constants in the button code if you want different ratios. Here’s the code:

<code>constant kTranslucencyRatio = 0.33
constant kGreyRatio = 0.15

on fadeImage pImageID
   local tAlphaData,tOffset,tWidth,tHeight,tAlpha
   lock screen
   put the width of pImageID into tWidth
   put the height of pImageID onto tHeight
   put the alphaData of pImageID into tAlphaData
   repeat with tOffset = 0 to tWidth *tHeight
      put charToNum(char tOffset of tAlphaData) into tAlpha
      -- handle cases of very low alpha for edge smoothing
      if round(tAlpha*kTranslucencyRatio) > 0 then
         put NumToChar(round(tAlpha*kTranslucencyRatio)) into char tOffset of tAlphaData
      end if
   end repeat
   set the alphaData of pImageID to tAlphaData
end fadeImage

on greyImage pImageID
   local tImageData, tRed, tGreen, tBlue, tGrey, tOffset,tWidth,tHeight
   lock screen
   put the width of pImageID into tWidth
   put the height of pImageID onto tHeight
   put the imageData of pImageID into tImageData
   repeat with tOffset = 0 to tWidth *tHeight * 4 step 4
      put charToNum(char tOffset + 2 of tImageData) into tRed
      put charToNum(char tOffset + 3 of tImageData) into tGreen
      put charToNum(char tOffset + 4 of tImageData) into tBlue
      put (30*tRed+59*tGreen+11*tBlue)/100 into tGrey
      put round(tGrey*kGreyRatio+tRed*(1-kGreyRatio)) into tRed
      put round(tGrey*kGreyRatio+tGreen*(1-kGreyRatio)) into tGreen
      put round(tGrey*kGreyRatio+tBlue*(1-kGreyRatio)) into tBlue
      put NumToChar(tRed) into char tOffset + 2 of tImageData
      put NumToChar(tGreen) into char tOffset + 3 of tImageData
      put NumToChar(tBlue) into char tOffset + 4 of tImageData
   end repeat
   set the imageData of pImageID to tImageData
end greyImage</code>

[dm]10[/dm]