DroidScript wiki

(was AndroidScript) unofficial documentation by the community

User Tools

Site Tools


sample_code:image_pixel_color

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

sample_code:image_pixel_color [2015/09/24 13:17] (current)
stevegarman created
Line 1: Line 1:
 +====== Image pixel colour ======
 +This sample shows the colour where you touch an image.
  
 +===== Sample Code =====
 +<code JavaScript pixel.js>​
 +
 +var txt,​orig;​
 +//​Called when application is started.
 +function OnStart() 
 +
 +    //​Create a layout with objects vertically centered. 
 +    var lay = app.CreateLayout( "​Linear",​ "​FillXY"​ );​     
 +     
 +    //​Create an image. 
 +    orig =  app.CreateImage( "/​Sys/​Img/​Xylophone.png"​ );​
 +    var img = app.CreateImage( "/​Sys/​Img/​Xylophone.png",​0.5,​-1 );​ 
 +
 +    img.SetOnTouchDown(img_OnTouchDown);​
 +    lay.AddChild( img );​ 
 +
 +    txt=app.CreateText("​Touch Xylophone"​);​
 +    lay.AddChild(txt);​
 +
 +    //​Add layout to app.     
 +    app.AddLayout( lay );​ 
 +    
 +
 +
 +function img_OnTouchDown(ev)
 +{
 +   var color = getPixelColor(this,​ev.X,​ev.Y);​
 +   txt.SetText(color);​
 +}
 +
 +function getPixelColor(img,​x,​y)
 +{
 +
 +var raw = img.GetPixelData( "​RawBase64",​
 +    x*img.GetAbsWidth()*orig.GetWidth()/​img.GetWidth(),​
 +    y*img.GetAbsHeight()*orig.GetHeight()/​img.GetHeight(),​1,​1);​
 +//​Decode the base64 encoded string
 +var byteArray = atob(raw);​
 +if (byteArray.length <​ 4) return("?"​);​
 +var r = byteArray.charCodeAt(0);​
 +var g = byteArray.charCodeAt(1);​
 +var b = byteArray.charCodeAt(2);​
 +var a = byteArray.charCodeAt(3);​
 +var col = "#"​ + toHex(a) + toHex(r) +toHex(g) + toHex(b);​
 +return col;​
 +}
 +
 +//​Return the 2 digit hex version of value,​ padded with
 +//​leading zeros when required
 +function toHex(value)
 +{
 +    return ("​00"​ + value.toString(16)).substr(-2);​
 +}
 +
 +</​code>​
sample_code/image_pixel_color.txt · Last modified: 2015/09/24 13:17 by stevegarman