Jump to content

Screen Color


jeantje
 Share

Recommended Posts

Hello guys I have a big problem

I am creating a ambi-light clone for my computer

but i need to set the lights to a color but what color

i can't use one pixel as color but i need to make a general RGB value

of all pixels of the screen but i really don't know how

Please help me

Greeting Jean

Link to comment
Share on other sites

  • Moderators

jeantje,

A quick search (the button is at the top right of the forum ;) ) produced this thread. I know it discusses bitmaps/jpg, but you could always use a ScreenCapture and work on that.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

jeantje,

A quick search (the button is at the top right of the forum ;) ) produced this thread. I know it discusses bitmaps/jpg, but you could always use a ScreenCapture and work on that.

M23

but that is a bit 2 slow in need to get the average color of the full screen with skip of 10 pixels

any ideas?

Link to comment
Share on other sites

  • Moderators

jeantje,

Edit: See post below for a better version. :evil:

Et voila - it takes about .5 sec per screen on my machine and the result looks pretty accurate: ;)

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <Color.au3>
#include <ScreenCapture.au3>

; Credit: Malkey for the basic GDI code

; Capture screen
_GDIPlus_Startup()
$hBMP = _ScreenCapture_Capture()
$hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBMP)

$iAverage_Colour = _Screen_Average_Colour($hImage)
ConsoleWrite($iAverage_Colour & @CRLF)

_GDIPlus_GraphicsDispose($hImage)
_GDIPlus_Shutdown()

; -------------

Func _Screen_Average_Colour($hImage2)

    Local $hBitmap1, $Reslt, $width, $height, $stride, $format, $Scan0, $v_Buffer, $v_Value, $iIW, $iIH

    Local $iBlue = 0, $iGreen = 0, $iRed = 0
    Local $iFractional_Blue = 0, $iFractional_Green = 0, $iFractional_Red = 0

    $iIW = _GDIPlus_ImageGetWidth($hImage2)
    $iIH = _GDIPlus_ImageGetHeight($hImage2)

    $hBitmap1 = _GDIPlus_BitmapCloneArea($hImage2, 0, 0, $iIW, $iIH, $GDIP_PXF32ARGB)

    $Reslt = _GDIPlus_BitmapLockBits($hBitmap1, 0, 0, $iIW, $iIH, BitOR($GDIP_ILMREAD, $GDIP_ILMWRITE), $GDIP_PXF32ARGB)

    ;Get the returned values of _GDIPlus_BitmapLockBits ()
    $width = DllStructGetData($Reslt, "width")
    $height = DllStructGetData($Reslt, "height")
    $stride = DllStructGetData($Reslt, "stride")
    ;$format = DllStructGetData($Reslt, "format")
    $Scan0 = DllStructGetData($Reslt, "Scan0")
    For $i = 0 To $iIW - 1 Step 10
        For $j = 0 To $iIH - 1 Step 10
            $v_Buffer = DllStructCreate("dword", $Scan0 + ($j * $stride) + ($i * 4))
            ; Get colour value of pixel
            $v_Value = DllStructGetData($v_Buffer, 1)
            ; Count
            $iBlue += _ColorGetBlue($v_Value)
            $iGreen += _ColorGetGreen($v_Value)
            $iRed += _ColorGetRed($v_Value)
        Next
        $iFractional_Blue += $iBlue * 10 / $iIH
        $iBlue = 0
        $iFractional_Green += $iGreen * 10 / $iIH
        $iGreen = 0
        $iFractional_Red += $iRed * 10 / $iIH
        $iRed = 0
    Next
    $avBlue = Hex(Round($iFractional_Blue * 10 / $iIW, 0), 2)
    $avGreen = Hex(Round($iFractional_Green * 10 / $iIW, 0), 2)
    $avRed = Hex(Round($iFractional_Red * 10 / $iIW, 0), 2)

    _GDIPlus_BitmapUnlockBits($hBitmap1, $Reslt)

    Return ($avRed & $avGreen & $avBlue)

EndFunc   ;==>_Screen_Average_Colour

If you want it any faster - write your own! :evil:

M23

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

jeantje,

Here is a more flexible version which allows you to choose the screen area to measure and the sample rate of the pixels: ;)

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <Color.au3>
#include <ScreenCapture.au3>

; Credit: Malkey for the basic GDI code

; Parameters: Pixel_Step, Left, Top, Width, Height - so this will average the top left hand 200x200 square measuring every 10 pixels
$iAverage_Colour = _Area_Average_Colour(10, 0, 0, 200, 200)
ConsoleWrite($iAverage_Colour & @CRLF)

; -------------

Func _Area_Average_Colour($iStep = 1, $iLeft = 0, $iTop = 0, $iWidth = @DesktopWidth, $iHeight = @DesktopHeight)

    Local $iBlue = 0, $iGreen = 0, $iRed = 0, $iInterim_Blue = 0, $iInterim_Green = 0, $iInterim_Red = 0, $iInner_Count = 0, $iOuter_Count = 0

    _GDIPlus_Startup()

    Local $hBMP = _ScreenCapture_Capture("", $iLeft, $iTop, $iLeft + $iWidth, $iTop + $iHeight)
    Local $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBMP)
    Local $hBitmap = _GDIPlus_BitmapCloneArea($hImage, 0, 0, $iWidth, $iHeight, $GDIP_PXF32ARGB)
    Local $tRes = _GDIPlus_BitmapLockBits($hBitmap, 0, 0, $iWidth, $iHeight, BitOR($GDIP_ILMREAD, $GDIP_ILMWRITE), $GDIP_PXF32ARGB)

    ;Get the returned values of _GDIPlus_BitmapLockBits()
    Local $iLock_Width  = DllStructGetData($tRes, "width")
    Local $iLock_Height = DllStructGetData($tRes, "height")
    Local $iLock_Stride = DllStructGetData($tRes, "stride")
    Local $iLock_Scan0  = DllStructGetData($tRes, "Scan0")

    ; Run through the BitMap testing pixels
    For $i = 0 To $iWidth - 1 Step $iStep
        For $j = 0 To $iHeight - 1 Step $iStep
            Local $v_Buffer = DllStructCreate("dword", $iLock_Scan0 + ($j * $iLock_Stride) + ($i * 4))
            ; Get colour value of pixel
            Local $v_Value = DllStructGetData($v_Buffer, 1)
            ; Add components
            $iBlue  += _ColorGetBlue($v_Value)
            $iGreen += _ColorGetGreen($v_Value)
            $iRed   += _ColorGetRed($v_Value)
            ; Adjust counter
            $iInner_Count += 1
        Next
        ; Determine average value so far - this prevents value becoming too large
        $iInterim_Blue  += $iBlue / $iInner_Count
        $iBlue = 0
        $iInterim_Green += $iGreen / $iInner_Count
        $iGreen = 0
        $iInterim_Red   += $iRed / $iInner_Count
        $iRed = 0
        ; Adjust counters
        $iInner_Count = 0
        $iOuter_Count += 1
    Next
    ; Determine final average
    Local $avBlue  = Hex(Round($iInterim_Blue / $iOuter_Count, 0), 2)
    Local $avGreen = Hex(Round($iInterim_Green / $iOuter_Count, 0), 2)
    Local $avRed   = Hex(Round($iInterim_Red / $iOuter_Count, 0), 2)

    ; Clear up
    _GDIPlus_BitmapUnlockBits($hBitmap, $tRes)
    _GDIPlus_GraphicsDispose($hImage)
    _GDIPlus_Shutdown()

    Return ($avRed & $avGreen & $avBlue)

EndFunc   ;==>_Area_Average_Colour

M23

Edit: Minor code change - order of parameters and added default values

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

jeantje,

Here is a more flexible version which allows you to choose the screen area to measure and the sample rate of the pixels: ;)

#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <Color.au3>
#include <ScreenCapture.au3>

; Credit: Malkey for the basic GDI code

; Parameters: Pixel_Step, Left, Top, Width, Height - so this will average the top left hand 200x200 square measuring every 10 pixels
$iAverage_Colour = _Area_Average_Colour(10, 0, 0, 200, 200)
ConsoleWrite($iAverage_Colour & @CRLF)

; -------------

Func _Area_Average_Colour($iStep = 1, $iLeft = 0, $iTop = 0, $iWidth = @DesktopWidth, $iHeight = @DesktopHeight)

    Local $iBlue = 0, $iGreen = 0, $iRed = 0, $iInterim_Blue = 0, $iInterim_Green = 0, $iInterim_Red = 0, $iInner_Count = 0, $iOuter_Count = 0

    _GDIPlus_Startup()

    Local $hBMP = _ScreenCapture_Capture("", $iLeft, $iTop, $iLeft + $iWidth, $iTop + $iHeight)
    Local $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBMP)
    Local $hBitmap = _GDIPlus_BitmapCloneArea($hImage, 0, 0, $iWidth, $iHeight, $GDIP_PXF32ARGB)
    Local $tRes = _GDIPlus_BitmapLockBits($hBitmap, 0, 0, $iWidth, $iHeight, BitOR($GDIP_ILMREAD, $GDIP_ILMWRITE), $GDIP_PXF32ARGB)

    ;Get the returned values of _GDIPlus_BitmapLockBits()
    Local $iLock_Width  = DllStructGetData($tRes, "width")
    Local $iLock_Height = DllStructGetData($tRes, "height")
    Local $iLock_Stride = DllStructGetData($tRes, "stride")
    Local $iLock_Scan0  = DllStructGetData($tRes, "Scan0")

    ; Run through the BitMap testing pixels
    For $i = 0 To $iWidth - 1 Step $iStep
        For $j = 0 To $iHeight - 1 Step $iStep
            Local $v_Buffer = DllStructCreate("dword", $iLock_Scan0 + ($j * $iLock_Stride) + ($i * 4))
            ; Get colour value of pixel
            Local $v_Value = DllStructGetData($v_Buffer, 1)
            ; Add components
            $iBlue  += _ColorGetBlue($v_Value)
            $iGreen += _ColorGetGreen($v_Value)
            $iRed   += _ColorGetRed($v_Value)
            ; Adjust counter
            $iInner_Count += 1
        Next
        ; Determine average value so far - this prevents value becoming too large
        $iInterim_Blue  += $iBlue / $iInner_Count
        $iBlue = 0
        $iInterim_Green += $iGreen / $iInner_Count
        $iGreen = 0
        $iInterim_Red   += $iRed / $iInner_Count
        $iRed = 0
        ; Adjust counters
        $iInner_Count = 0
        $iOuter_Count += 1
    Next
    ; Determine final average
    Local $avBlue  = Hex(Round($iInterim_Blue / $iOuter_Count, 0), 2)
    Local $avGreen = Hex(Round($iInterim_Green / $iOuter_Count, 0), 2)
    Local $avRed   = Hex(Round($iInterim_Red / $iOuter_Count, 0), 2)

    ; Clear up
    _GDIPlus_BitmapUnlockBits($hBitmap, $tRes)
    _GDIPlus_GraphicsDispose($hImage)
    _GDIPlus_Shutdown()

    Return ($avRed & $avGreen & $avBlue)

EndFunc   ;==>_Area_Average_Colour

M23

Edit: Minor code change - order of parameters and added default values

But it's still to slow and it crashes on my Acer Aspire One maybe the atom cpu fucks it up?
Link to comment
Share on other sites

  • Moderators

jeanjte,

If reading a full screen of pixels for average colour in about .5 sec is too slow for you - read the last line in my post above: :evil:

If you want it any faster - write your own!

This is AutoIt we are talking about remember. You should think yourself lucky that you are coding in something which can even do this using built-in commands! And even luckier that someone else wrote the code for you!

Some people! ;)

M23

P.S. And I have no idea why it crashes on your machine. I have posted a more developed version in the Examples forum and no-one else has complained yet.

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...