Color Changing Gui background Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
RDC
Posts: 112
Joined: 29 Jan 2023, 10:22

Color Changing Gui background

Post by RDC » 19 Mar 2023, 21:26

I made this script to randomly change the background color at a set rate of time. Currently I have it at 2.5 seconds.
I'm hoping someone will know how to alter this script to make the color transitions smooth rather than as snapshots?

Code: Select all

#SingleInstance, Force
#NoEnv
SetBatchLines, -1

Text= Some text for contrast.
Gui, 
    -Caption
    +LastFound
    +AlwaysOnTop
    +Resize

Gui, Color, FFFFFF
Gui, Margin, 10, 30
Gui, Font, s18 BOLD q5, Verdana
Gui, Add,Text,x22 y22 Center vText1 c000000 BackgroundTrans, % Text
Gui, Add,Text,x20 y20 Center vText2 cFFFF00 BackgroundTrans, % Text
Gui, Show, NoActivate

Loop
{
    Random, color, 0, 0xFFFFFF
    Gui Color, %color%
    Sleep, 2500
}

;
;  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶ ̶  ̶  ̶  ̶  
RETURN
^!R::Reload 	 ; (Ctrl+Alt+R)
^Esc::
     Gui, Destroy 	 ; 
     ExitApp 	 ; (Ctrl+Esc)

Rohwedder
Posts: 7555
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Color Changing Gui background

Post by Rohwedder » 20 Mar 2023, 03:36

Hallo,
try:

Code: Select all

#SingleInstance, Force
#NoEnv
SetBatchLines, -1
Text = Some text for contrast.
Loop
{
	Random, Color, 0, 0xFFFFFF
	IDOld := IDNew
	Gui, New, +HwndIDNew
	-Caption
	+LastFound
	+Resize
	+ToolWindow 
	Gui, Color,% Color
	Gui, Margin, 10, 30
	Gui, Font, s18 BOLD q5, Verdana
	Gui, Add,Text,x22 y22 Center vText1 c000000 BackgroundTrans, % Text
	Gui, Add,Text,x20 y20 Center vText2 cFFFF00 BackgroundTrans, % Text
	Gui, Show, NoActivate
	WinSet, Transparent, 0, ahk_id %IDNew%
	Gui, +AlwaysOnTop
	Loop, 10
	{
		Sleep, 50
		WinSet, Transparent,% A_Index*25.5, ahk_id %IDNew%		
	}
	WinClose, ahk_id %IDOld%
	Sleep, 2000
}
;
;  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶ ̶  ̶  ̶  ̶
RETURN
^!R::Reload 	 ; (Ctrl+Alt+R)
^Esc::
WinClose, ahk_id %IDNew% 	 ;
ExitApp 	 ; (Ctrl+Esc)

off
Posts: 176
Joined: 18 Nov 2022, 21:54

Re: Color Changing Gui background

Post by off » 20 Mar 2023, 05:57

@Rohwedder
Cool script
But when i resize the window, it will just go back to the original size
image.png
image.png (15.77 KiB) Viewed 470 times
My Creations
IMG2HotString - Send image file easily with your hotstring!
CtrlSend - A small solution for sending keys to window in background that doesn't accept ControlSend's key
ControlProcess

Rohwedder
Posts: 7555
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Color Changing Gui background  Topic is solved

Post by Rohwedder » 20 Mar 2023, 06:29

Then:

Code: Select all

#SingleInstance, Force
#NoEnv
SetBatchLines, -1
Text = Some text for contrast.
Loop
{
	Random, Color, 0, 0xFFFFFF
	IF IDOld := IDNew
	{
		VarSetCapacity(WH, 16)
		DllCall("GetClientRect", "Ptr", IDNew, "Ptr", &WH)
		NewWidth := NumGet(WH, 8, "Int")
		NewHeight := NumGet(WH, 12, "Int")
		WinGetPos, NewX, NewY,,, ahk_id %IDNew%
		PosNew = X%NewX% Y%NewY% W%NewWidth% H%NewHeight%
	}
	Gui, New, +HwndIDNew
	-Caption
	+LastFound
	+Resize
	+ToolWindow
	-DPIScale
	Gui, Color,% Color
	Gui, Margin, 10, 30
	Gui, Font, s18 BOLD q5, Verdana
	Gui, Add,Text,x22 y22 Center vText1 c000000 BackgroundTrans, % Text
	Gui, Add,Text,x20 y20 Center vText2 cFFFF00 BackgroundTrans, % Text
	Gui, Show, NoActivate %PosNew%
	WinSet, Transparent, 0, ahk_id %IDNew%
	Gui, +AlwaysOnTop
	Loop, 10
	{
		Sleep, 50
		WinSet, Transparent,% A_Index*25.5, ahk_id %IDNew%
	}
	WinClose, ahk_id %IDOld%
	Sleep, 2000
}
;
;  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶  ̶ ̶  ̶  ̶  ̶
RETURN
^!R::Reload 	 ; (Ctrl+Alt+R)
^Esc::
WinClose, ahk_id %IDNew% 	 ;
ExitApp 	 ; (Ctrl+Esc)

User avatar
RDC
Posts: 112
Joined: 29 Jan 2023, 10:22

Re: Color Changing Gui background

Post by RDC » 20 Mar 2023, 07:34

Thank you for this Rohwedder. It is exactly what I was trying to do!


Edit: Thanks again. This really does work awesome! Sadly it is a couple levels or so above my learning so far so I'm having to research how the different things work. It's a slow process, but I eventually learn this stuff that way.
I was hoping you could show how to make this work just for select color hues, like only blue hues or maybe even a between a couple of hues between red and yellow? Not sure that's even possible, but I have to at least ask.
Many thanks once again!!
Gui Colors.gif
Gui Colors.gif (587.56 KiB) Viewed 442 times
Example of Rohwedder's improvement!
Forgive the textured appearance. That speaks to my screen recorder quality.

Rohwedder
Posts: 7555
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Color Changing Gui background

Post by Rohwedder » 20 Mar 2023, 11:57

This script simply uses crossfading technique.
Each time a second gui (IDNew) is created below the first (IDOld), made invisible and then brought in front of the first.
Then the second gui becomes increasingly visible in 10 steps and finally the first gui will be closed. Where both guis differ, the colors mix.
If, for example, only the blue values are to change, then make sure that the guis only differ in the blue values.

User avatar
Hellbent
Posts: 2103
Joined: 23 Sep 2017, 13:34

Re: Color Changing Gui background

Post by Hellbent » 20 Mar 2023, 13:18

RDC wrote:
20 Mar 2023, 07:34

I was hoping you could show how to make this work just for select color hues, like only blue hues or maybe even a between a couple of hues between red and yellow? Not sure that's even possible, but I have to at least ask.
Many thanks once again!!

Here are a few functions that you can check out that will allow you to use HSL.

Code: Select all

;**************************************
HSL_ToRGB(hue, sat:=1, lum:=0.5 ) {
; Function by [VxE]. See > http://www.wikipedia.org/wiki/HSV_color_space
; HSL to/from RGB conversion functions by [VxE]. Freely avalable @ http://www.autohotkey.com/community/viewtopic.php?f=2&t=88707
; Converts a hue/sat/lum into a 24-bit RGB color code. Input: 0 <= hue <= 360, 0 <= sat <= 1, 0 <= lum <= 1. 

   Static i24 := 0xFFFFFF, i40 := 0xFFFFFF0000, hx := "0123456789ABCDEF"

; Transform the decimal inputs into 24-bit integers. Integer arithmetic is nice..
   sat := ( sat * i24 ) & i24
   lum := ( lum * i24 ) & i24
   hue := ( hue * 0xB60B60 >> 8 ) & i24 ; conveniently, 360 * 0xB60B60 = 0xFFFFFF00

; Determine the chroma value and put it in the 'sat' var since the saturation value is not used after this.
   sat := lum + Round( sat * ( i24 - Abs( i24 - lum - lum ) ) / 0x1FFFFFE )

; Calculate the base values for red and blue (green's base value is the hue)
   red := hue < 0xAAAAAA ? hue + 0x555555 : hue - 0xAAAAAA
   blu := hue < 0x555555 ? hue + 0xAAAAAA : hue - 0x555555

; Run the blue value through the cases
   If ( blu < 0x2AAAAB )
      blu := sat + 2 * ( i24 - 6 * blu ) * ( lum - sat ) / i24 >> 16
   Else If ( blu < 0x800000 )
      blu := sat >> 16
   Else If ( blu < 0xAAAAAA )
      blu := sat + 2 * ( i24 - 6 * ( 0xAAAAAA - blu ) ) * ( lum - sat ) / i24 >> 16
   Else
      blu := 2 * lum - sat >> 16

; Run the red value through the cases
   If ( red < 0x2AAAAB )
      red := sat + 2 * ( i24 - 6 * red ) * ( lum - sat ) / i24 >> 16
   Else If ( red < 0x800000 )
      red := sat >> 16
   Else If ( red < 0xAAAAAA )
      red := sat + 2 * ( i24 - 6 * ( 0xAAAAAA - red ) ) * ( lum - sat ) / i24 >> 16
   Else
      red := 2 * lum - sat >> 16

; Run the green value through the cases
   If ( hue < 0x2AAAAB )
      hue := sat + 2 * ( i24 - 6 * hue ) * ( lum - sat ) / i24 >> 16
   Else If ( hue < 0x800000 )
      hue := sat >> 16
   Else If ( hue < 0xAAAAAA )
      hue := sat + 2 * ( i24 - 6 * ( 0xAAAAAA - hue ) ) * ( lum - sat ) / i24 >> 16
    Else
      hue := 2 * lum - sat >> 16

; Return the values in RGB as a hex integer
   Return "0x" SubStr( hx, ( red >> 4 ) + 1, 1 ) SubStr( hx, ( red & 15 ) + 1, 1 )
         . SubStr( hx, ( hue >> 4 ) + 1, 1 ) SubStr( hx, ( hue & 15 ) + 1, 1 )
         . SubStr( hx, ( blu >> 4 ) + 1, 1 ) SubStr( hx, ( blu & 15 ) + 1, 1 )
} ; END - HSL_ToRGB( hue, sat, lum )

;*****************************************************

HSL_Lum( RGB ) {
; Function by [VxE]. Returns the HSL lightness of the input 24 bit color.
; Returns a floating point value between 0 and 1, inclusive.

   blu := 255 & ( RGB )
   grn := 255 & ( RGB >> 8 )
   red := 255 & ( RGB >> 16 )

   If ( blu < grn )
   {
      If ( red < blu )
         Return ( red + grn ) / 510
      Else If ( grn < red )
         Return ( blu + red ) / 510
      Else
         Return ( blu + grn ) / 510
   } Else
   {
      If ( red < grn )
         Return ( red + blu ) / 510
      Else If ( blu < red )
         Return ( grn + red ) / 510
      Else
         Return ( grn + blu ) / 510
   }
} ; END - HSL_Lum( RGB )

;*****************************************************

ConvertRGBtoHSL(R, G, B) {
; http://www.easyrgb.com/index.php?X=MATH&H=18#text18
   SetFormat, float, 0.5 ; for some reason I need this for some colors to work.

   R := (R / 255)
   G := (G / 255)
   B := (B / 255)

   Min     := min(R, G, B)
   Max     := max(R, G, B)
   del_Max := Max - Min

   L := (Max + Min) / 2

   if (del_Max = 0)
   {
      H := S := 0
   } else
   {
      if (L < 0.5)
         S := del_Max / (Max + Min)
      else
         S := del_Max / (2 - Max - Min)

      del_R := (((Max - R) / 6) + (del_Max / 2)) / del_Max
      del_G := (((Max - G) / 6) + (del_Max / 2)) / del_Max
      del_B := (((Max - B) / 6) + (del_Max / 2)) / del_Max

      if (R = Max)
      {
         H := del_B - del_G
      } else
      {
         if (G = Max)
            H := (1 / 3) + del_R - del_B
         else if (B = Max)
            H := (2 / 3) + del_G - del_R
      }
      if (H < 0)
         H += 1
      if (H > 1)
         H -= 1
   }
   ; return round(h*360) "," s "," l
   ; return (h*360) "," s "," l
   return [abs(round(h*360, 3)), abs(s), abs(l)]
}
;*******************************************************************
ConvertHSLtoRGB(H, S, L) {
; http://www.had2know.com/technology/hsl-rgb-color-converter.html

   H := H/360
   if (S == 0)
   {
      R := L*255
      G := L*255
      B := L*255
   } else
   {
      if (L < 0.5)
         var_2 := L * (1 + S)
      else
         var_2 := (L + S) - (S * L)
      var_1 := 2 * L - var_2

      R := 255 * ConvertHueToRGB(var_1, var_2, H + (1 / 3))
      G := 255 * ConvertHueToRGB(var_1, var_2, H)
      B := 255 * ConvertHueToRGB(var_1, var_2, H - (1 / 3))
   }
   ; Return round(R) "," round(G) "," round(B)
   ; Return (R) "," (G) "," (B)
   Return [round(R), round(G), round(B)]
}

;********************************************************************
ConvertHueToRGB(v1, v2, vH) {
   vH := ((vH<0) ? ++vH : vH)
   vH := ((vH>1) ? --vH : vH)
   return  ((6 * vH) < 1) ? (v1 + (v2 - v1) * 6 * vH)
         : ((2 * vH) < 1) ? (v2)
         : ((3 * vH) < 2) ? (v1 + (v2 - v1) * ((2 / 3) - vH) * 6)
         : v1
}
;**********************************************************************
RGBtoHEXandBack(clr) {
   If (StrLen(clr)=9)
   {
      R := SubStr(clr, 1, 3)
      G := SubStr(clr, 4, 3)
      B := SubStr(clr, 7, 3)
      z := Format("{1:02x}", R) Format("{1:02x}", G) Format("{1:02x}", B)
  } Else
  {
      R := "0x" SubStr(clr, 1, 2)
      G := "0x" SubStr(clr, 3, 2)
      B := "0x" SubStr(clr, 5, 2)
      z := Format("{1:03d}", R) Format("{1:03d}", G) Format("{1:03d}", B)
  }

  Return z
}

Post Reply

Return to “Ask for Help (v1)”