Re: Get RGB color of a specific screen location

2019-01-09 Thread hh via use-livecode
Looking again at your problem my experiences with both statistics
and images say that the *median* would be a better average than
the *arithmeticMean*. So you could try and compare both averages
(more exactly: location parameters of your WxH-color-distribution).

local t="temp"

-- hL,vL is the topleft of the pixel-rect on your screen
-- the width  of the pixel-rect is w. W is an integer >= 1.
-- the height of the pixel-rect is h. H is an integer >= 1.
-- tp is the average method "median" or (default:) "arithmeticMean"
-- average is taken from the pixel-colors of rect (hL,vL,hL+w,vL+h)
--
function averageColor w,h,hL,vL,tp
   export snapshot from rect (hL,vL,hL+w,vL+h) to img t as PNG
   put the imagedata of img t into iData
   put the alphadata of img t into aData
   repeat with i=1 to w*h
  put byteToNum(byte 4*i-2 of iData) into redA[i]
  put byteToNum(byte 4*i-1 of iData) into greenA[i]
  put byteToNum(byte 4*i   of iData) into blueA[i]
  put byteToNum(byte i of aData) into alphaA[i]
   end repeat
   if tp is "median" then
  return trunc(median(redA)),trunc(median(greenA)), \
 trunc(median(blueA)),trunc(median(alphaA))
   else
  return trunc(avg(redA)),trunc(avg(greenA)), \
 trunc(avg(blueA)),trunc(avg(alphaA))
   end if
end averageColor

-- usage example:
on mouseUp
   lock screen; lock messages
   put 5 into w0; put 5 into h0
   put -w0+the left of this stack into sL 
   put -h0+the top of this stack into sT -- = without window bar
   
   if there is no img "temp" then create invisible img t
   if there is no field t then
  create fld t
  set rect of fld t to (0,0,128,23)
  set botleft of fld t to the botright of me
   end if
   if there is no grc t then
  create grc t
  set opaque of grc t to true
  set rect of grc t to (0,0,23,23)
  set botright of grc t to the botleft of me
   end if
   
   put averageColor(w0,h0,sL,sT,"median") into x --> returns r,g,b,alpha
    use x, for example:
   set backColor of grc t to item 1 to 3 of x
   --set blendlevel of grc t to round(100*(1-(item 4 of x)/255)) -- if wanted
   put x into fld t
   unlock screen; unlock messages
end mouseUp


___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode


Re: Get RGB color of a specific screen location

2019-01-09 Thread Beat Cornaz via use-livecode
I have used the export snapshot and then the averaging. Works great. I had
forgotten about the export function (have been away from coding for 3
years).
I also had to implement Phil Davis' "set the screenMouseLoc to
globalLoc(tLocWithinMyStack)".
The average I got first (with only the mouseLoc) was offsetted to where I
clicked. Now it purs :-)

Thanks again, Beat
___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode


Re: Get RGB color of a specific screen location

2019-01-09 Thread Beat Cornaz via use-livecode
Thanks everyone for the many suggestions. I will try them out and see which
one will work best.
Appreciate the multitude of ideas.

Cheers, Beat
___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode


Re: Get RGB color of a specific screen location

2019-01-07 Thread Håkan Liljegren via use-livecode
Well, he could, if he didn’t wanted an average value of some more pixels, and, 
calculating an average via screenMouseLoc is really slow. Exporting a rect to 
an image is fairly quick though

Found the following in a stack:

# Calculates the average color value for an image
# pID should be the long id of the image we want to get the average from
function imageAverageValue pID
   put the imageData of pID into tData
   put the number of bytes of tData / 4 into tNumPixels
   # Sum all pixel values
   repeat with i = 0 to tNumPixels - 1
      add byteToNum(byte i*4 + 2 of tData) to tResult["red"]
      add byteToNum(byte i*4 + 3 of tData) to tResult["green"]
      add byteToNum(byte i*4 + 4 of tData) to tResult["blue"]
   end repeat
   # Calculate mean value
   repeat for each item anItem in "red,green,blue"
      put round(tResult[anItem] / tNumPixels) into tResult[anItem]
   end repeat
   return tResult["red"], tResult["green"], tResult["blue"]
end imageAverageValue

So to get the mouse average color in a square ± 2px from the mouseLoc we could 
do:

function getMouseAverageColor pSize
   if pSize is empty then put 2 into pSize
   put globalLoc(the mouseLoc) into tLoc
   if there is no image "averageColor" then create invisible image 
"averageColor"
   put the long id of image "averageColor" into tID
   export snapshot from rectangle item 1 of tLoc - pSize, item 2 of tLoc - 
pSize,  \
         item 1 of tLoc + pSize, item 2 of tLoc + pSize to image "averageColor"
   return imageAverageValue(tID)
end getMouseAvaregeColor

Happy coding!

:-Håkan
On 7 Jan 2019, 15:35 +0100, dunbarxx via use-livecode 
, wrote:
> A kluge, but couldn't you:
>
> on mouseUp
> set the screenMouseLoc to "200,200"
> answer the mouseColor
> end mouseUp
>
> I just threw this together, so you might want to restore the original
> mouseLoc, and locking the screen might be useful.
>
> Craig Newman
>
>
>
> --
> Sent from: 
> http://runtime-revolution.278305.n4.nabble.com/Revolution-User-f278306.html
>
> ___
> use-livecode mailing list
> use-livecode@lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription 
> preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode

Re: Get RGB color of a specific screen location

2019-01-07 Thread hh via use-livecode
Sorry, my last post had a typo. Now it is correct.

> Beat C. wrote:
> What I need is to get the RGB of a specific screen
> location - I need to make an avarage of e.g. 5x5 pixels.

Make a new small stack with a button. Script it as given below.
On mouseUp the average is taken from the 5x5-rect left of the topleft
of your stack.

- begin button script
local t="temp"

-- hL,vL is the topleft of the pixel-rect on your screen.
-- the width  of the pixel-rect is w. W is an integer >= 1.
-- the height of the pixel-rect is h. H is an integer >= 1.
-- average color is taken from the pixel-colors of rect (hL,vL,hL+w,vL+h)
function averageColor w,h,hL,vL
  export snapshot from rect (hL,vL,hL+w,vL+h) to img t as PNG
  put the rect of img t &" / "  width of img t,the height of img t into fld 
"OUT"
  put the imagedata of img t into iData
  put the alphadata of img t into aData
  repeat with i=1 to w*h
 put byteToNum(byte 4*i-2 of iData) into redA[i]
 put byteToNum(byte 4*i-1 of iData) into greenA[i]
 put byteToNum(byte 4*i   of iData) into blueA[i]
 put byteToNum(byte i of aData) into alphaA[i]
  end repeat
  return 
trunc(avg(redA)),trunc(avg(greenA)),trunc(avg(blueA)),trunc(avg(alphaA))
end averageColor

on mouseUp
  lock screen; lock messages
  put 5 into w0; put 5 into h0
  put -w0+the left of this stack into sL 
  put -h0+the top of this stack into sT -- = without window bar
  
  if there is no img "temp" then create invisible img t
  if there is no field t then
 create fld t
 set rect of fld t to (0,0,128,23)
 set botleft of fld t to the botright of me
  end if
  if there is no grc t then
 create grc t
 set opaque of grc t to true
 set rect of grc t to (0,0,23,23)
 set botright of grc t to the botleft of me
  end if
  
  put averageColor(w0,h0,sL,sT) into x --> returns r,g,b,alpha
   use x, for example:
  set backColor of grc t to item 1 to 3 of x
  -- set blendlevel of grc t to round(100*(1-(item 4 of x)/255)) -- if wanted
  put x into fld t
  unlock screen; unlock messages
end mouseUp

--- end button script
___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode


Re: Get RGB color of a specific screen location

2019-01-07 Thread hh via use-livecode
Sorry, my last post had a typo. Now it is correct.

> Beat C. wrote:
> What I need is to get the RGB of a specific screen
> location - I need to make an avarage of e.g. 5x5 pixels.

Make a new small stack with a button. Script it as given below.
On mouseUp the average is taken from the 5x5-rect left of the topleft
of your stack.

- begin button script
local t="temp"

-- hL,vL is the topleft of the pixel-rect on your screen.
-- the width  of the pixel-rect is w. W is an integer >= 1.
-- the height of the pixel-rect is h. H is an integer >= 1.
-- average color is taken from the pixel-colors of rect (hL,vL,hL+w,vL+h)
function averageColor w,h,hL,vL
 export snapshot from rect (hL,vL,hL+w,vL+h) to img t as PNG
 put the rect of img t &" / "  width of img t,the height of img t into fld 
"OUT"
 put the imagedata of img t into iData
 put the alphadata of img t into aData
 repeat with i=1 to w*h
put byteToNum(byte 4*i-2 of iData) into redA[i]
put byteToNum(byte 4*i-1 of iData) into greenA[i]
put byteToNum(byte 4*i   of iData) into blueA[i]
put byteToNum(byte i of aData) into alphaA[i]
 end repeat
 return trunc(avg(redA)),trunc(avg(greenA)),trunc(avg(blueA)),trunc(avg(alphaA))
end averageColor

on mouseUp
 lock screen; lock messages
 put 5 into w0; put 5 into h0
 put -w0+the left of this stack into sL 
 put -h0+the top of this stack into sT -- = without window bar
 
 if there is no img "temp" then create invisible img t
 if there is no field t then
create fld t
set rect of fld t to (0,0,128,23)
set botleft of fld t to the botright of me
 end if
 if there is no grc t then
create grc t
set opaque of grc t to true
set rect of grc t to (0,0,23,23)
set botright of grc t to the botleft of me
 end if
 
 put averageColor(w0,h0,sL,sT) into x --> returns r,g,b,alpha
  use x, for example:
 set backColor of grc t to item 1 to 3 of x
 -- set blendlevel of grc t to round(100*(1-(item 4 of x)/255)) -- if wanted
 put x into fld t
 unlock screen; unlock messages
end mouseUp

--- end button script
___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode


Re: Get RGB color of a specific screen location

2019-01-07 Thread hh via use-livecode
> Beat C. wrote:
> What I need is to get the RGB of a specific screen
> location - I need to make an avarage of e.g. 5x5 pixels.

Make a new small stack with a button. Script it as given below.
On mouseUp the average is taken from the 5x5-rect left of the topleft
of your stack.

- begin button script
local t="temp"

-- hL,vL is the topleft of the pixel-rect on your screen
-- the width  of the pixel-rect is w. W is an integer >= 1.
-- the height of the pixel-rect is h. H is an integer >= 1.
-- average color is taken from the pixel-colors of rect (hL,vL,hL+w,vL+h)
function averageColor w,h,hL,vL
   export snapshot from rect (hL,vL,hL+w,vL+h) to img t as PNG
   put the rect of img t &" / "  width of img t,the height of img t into 
fld "OUT"
   put the imagedata of img t into iData
   put the imagedata of img t into aData
   repeat with i=1 to w*h
  put byteToNum(byte 4*i-2 of iData) into redA[i]
  put byteToNum(byte 4*i-1 of iData) into greenA[i]
  put byteToNum(byte 4*i   of iData) into blueA[i]
  put byteToNum(byte i of aData) into alphaA[i]
   end repeat
   return 
trunc(avg(redA)),trunc(avg(greenA)),trunc(avg(blueA)),trunc(avg(alphaA))
end averageColor

on mouseUp
   lock screen; lock messages
   put 5 into w0; put 5 into h0
   put -w0+the left of this stack into sL 
   put -h0+the top of this stack into sT -- = without window bar
   
   if there is no img "temp" then create invisible img t
   if there is no field t then
  create fld t
  set rect of fld t to (0,0,128,23)
  set botleft of fld t to the botright of me
   end if
   if there is no grc t then
  create grc t
  set opaque of grc t to true
  set rect of grc t to (0,0,23,23)
  set botright of grc t to the botleft of me
   end if
   
   put averageColor(w0,h0,sL,sT) into x --> returns r,g,b,alpha
    use x, for example:
   set backColor of grc t to item 1 to 3 of x
   -- set blendlevel of grc t to round(100*(1-(item 4 of x)/255)) -- if wanted
   put x into fld t
   unlock screen; unlock messages
end mouseUp

--- end button script
___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode


Re: Get RGB color of a specific screen location

2019-01-07 Thread hh via use-livecode
Try

if there is no img "temp" then create invisible img "temp"
export snapshot from rect  to img "temp" as PNG

Then average the imagedata of that image. If you would
like to use transparency then also use the alphadata.

This is for desktop, on mobile there are some specials
(see the dictionary).

> I have asked this before, but for the first time did not
> get any answer on my post.

There is also the forum http://forums.livecode.com !!

___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode


Re: Get RGB color of a specific screen location

2019-01-07 Thread dunbarxx via use-livecode
A kluge, but couldn't you:

on mouseUp
   set the screenMouseLoc to "200,200"
   answer the mouseColor
end mouseUp

I just threw this together, so you might want to restore the original
mouseLoc, and locking the screen might be useful.

Craig Newman



--
Sent from: 
http://runtime-revolution.278305.n4.nabble.com/Revolution-User-f278306.html

___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode


Re: Get RGB color of a specific screen location

2019-01-07 Thread hh via use-livecode
Try

if there is no img "temp" then create invisible img "temp"
export snapshot from rect  to img "temp" as PNG

Then average the imagedata of that image. If you would
like to use transparency then also use the alphadata.

This is for desktop, on mobile there are some specials
(see the dictionary).

> I have asked this before, but for the first time did not
> get any answer on my post.

There is also the forum http://forums.livecode.com !!

___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode


Re: Get RGB color of a specific screen location

2019-01-07 Thread Lagi Pittas via use-livecode
Seems like it was answered the same day by Phil Davis

Lagi

Phil Davis via use-livecode
Mon, 31 Dec 2018, 17:51 (7 days ago)
to Beat, Phil
set the screenMouseLoc to globalLoc(tLocWithinMyStack)
put the mouseColor into tTheColorAtThatScreenLoc

Phil Davis

On Mon, 7 Jan 2019 at 12:10, Beat Cornaz via use-livecode <
use-livecode@lists.runrev.com> wrote:

> I have asked this befor, but for the first time did not get any answer on
> my post. So I hope you don't mind that I try again :-)
>
> How do I get the RGB color of a pixel of an image? I know the function the
> MouseColor, which gives me the RGB numbers under the cursor. What I need is
> to get the RGB of a specific screen location - I need to make an avarage of
> e.g. 5x5 pixels. So how do I get the RGB numbers of e.g. location
> "30,200"?
>
> Cheers, Beat
> ___
> use-livecode mailing list
> use-livecode@lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>
___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode


Re: Get RGB color of a specific screen location

2018-12-31 Thread Phil Davis via use-livecode

set the screenMouseLoc to globalLoc(tLocWithinMyStack)
put the mouseColor into tTheColorAtThatScreenLoc

Phil Davis


On 12/31/18 9:24 AM, Beat Cornaz via use-livecode wrote:

How do I get the RGB color of a pixel of an image? I know the function the
MouseColor, which gives me the RGB numbers under the cursor. What I need is
to get the RGB of a specific screen location - I need to make an avarage of
e.g. 5x5 pixels. So how do I get the RGB numbers of e.g. location  "30,200"?

Cheers, Beat
___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode




--
Phil Davis


___
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode