graphics: rainbow colours

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

graphics: rainbow colours

21 Jul 2017, 22:06

If there is a better mathematical function for converting wavelength (nanometres) to RGB colour values, I would be glad to know. Thanks.

Code: Select all

;requires Gdip.ahk or Gdip_All.ahk
q:: ;rainbow colours - create image bar (wavelength to RGB)
vList := "1,2,4,5,10"
pToken := Gdip_Startup()
vRange1 := 380, vRange2 := 780
vMax := vRange2-vRange1+1
Loop, Parse, vList, % ","
{
	vInc := A_LoopField ;increment value
	;maths: use the first number and every nth number after that
	;maths e.g. 'range 1-20, jump 5', imagine it is 0-19, giving: 0,5,10,15 (so count is: 1 + Floor(19/5) = 4)
	;maths e.g. 'range 1-21, jump 5', imagine it is 0-20, giving: 0,5,10,15,20 (so count is: 1 + Floor(20/5) = 5)
	vImgW := Floor((vMax-1)/vInc)+1
	vImgH := 100
	pBitmap := Gdip_CreateBitmap(vImgW, vImgH)
	vNum := vRange1, vCount := 0
	Loop
	{
		if (vNum > vRange2)
			break
		JEE_WavelengthToRGB(vNum, vColR, vColG, vColB)
		vColRGB := Format("0xFF{:02X}{:02X}{:02X}", vColR, vColG, vColB)
		vPosX := A_Index-1
		Loop, % vImgH
			Gdip_SetPixel(pBitmap, vPosX, A_Index-1, vColRGB)
		vCount++
		vNum += vInc
	}
	if !(vCount = vImgW)
		MsgBox, % "warning: image width miscalculated: " vCount " " vImgW
	vPath = %A_Desktop%\z rainbow %vInc%.png
	if !(FileExist, vPath)
		Gdip_SaveBitmapToFile(pBitmap, vPath, 100)
	Gdip_DisposeImage(pBitmap)
}
Gdip_Shutdown(pToken)
MsgBox, % "done"
return

;==================================================

;based on:
;efg's Computer Lab: Spectra Lab Report
;http://www.efg2.com/Lab/ScienceAndEngineering/Spectra.htm
;for generating rainbow colours
;range: 380 <= vWavelength (nanometres) < 781
JEE_WavelengthToRGB(vWavelength, ByRef vRed, ByRef vGreen, ByRef vBlue)
{
	vGamma := 0.8, vIntensityMax := 255
	if (vWavelength >= 380) && (vWavelength < 440)
		vRed := -(vWavelength - 440) / (440 - 380), vGreen := 0, vBlue := 1
	else if (vWavelength >= 440) && (vWavelength < 490)
		vRed := 0, vGreen := (vWavelength - 440) / (490 - 440), vBlue := 1
	else if (vWavelength >= 490) && (vWavelength < 510)
		vRed := 0, vGreen := 1, vBlue := -(vWavelength - 510) / (510 - 490)
	else if (vWavelength >= 510) && (vWavelength < 580)
		vRed := (vWavelength - 510) / (580 - 510), vGreen := 1, vBlue := 0
	else if (vWavelength >= 580) && (vWavelength < 645)
		vRed := 1, vGreen := -(vWavelength - 645) / (645 - 580), vBlue := 0
	else if (vWavelength >= 645) && (vWavelength < 781)
		vRed := 1, vGreen := 0, vBlue := 0
	else
		vRed := 0, vGreen := 0, vBlue := 0

	if (vWavelength >= 380) && (vWavelength < 420)
		vFactor := 0.3 + 0.7*(vWavelength - 380) / (420 - 380)
	else if (vWavelength >= 420) && (vWavelength < 701)
		vFactor := 1
	else if (vWavelength >= 701) && (vWavelength < 781)
		vFactor := 0.3 + 0.7*(780 - vWavelength) / (780 - 700)
	else
		vFactor := 0

	(vRed != 0) && vRed := Round(vIntensityMax * ((vRed*vFactor)**vGamma))
	(vGreen != 0) && vGreen := Round(vIntensityMax * ((vGreen*vFactor)**vGamma))
	(vBlue != 0) && vBlue := Round(vIntensityMax * ((vBlue*vFactor)**vGamma))
}
Last edited by jeeswg on 10 Aug 2017, 14:45, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: graphics: rainbow colours

06 Aug 2017, 06:02

A similar script which produces red/white/blue colours based on:
How to Convert Temperature (K) to RGB: Algorithm and Sample Code – Tanner Helland (dot) com
http://www.tannerhelland.com/4435/conve ... ithm-code/

Code: Select all

;requires Gdip.ahk or Gdip_All.ahk
q:: ;temperature to RGB
;vList := "100", vRange1 := 0, vRange2 := 40000 ;0 K to 40000 K
vList := "100", vRange1 := 1000, vRange2 := 40000 ;1000 K to 40000 K
;vList := "100", vRange1 := 1500, vRange2 := 15000 ;1500 K to 15000 K
pToken := Gdip_Startup()
vMax := vRange2-vRange1+1
Loop, Parse, vList, % ","
{
	vInc := A_LoopField ;increment value
	;maths: use the first number and every nth number after that
	;maths e.g. 'range 1-20, jump 5', imagine it is 0-19, giving: 0,5,10,15 (so count is: 1 + Floor(19/5) = 4)
	;maths e.g. 'range 1-21, jump 5', imagine it is 0-20, giving: 0,5,10,15,20 (so count is: 1 + Floor(20/5) = 5)
	vImgW := Floor((vMax-1)/vInc)+1
	vImgH := 100
	pBitmap := Gdip_CreateBitmap(vImgW, vImgH)
	vNum := vRange1, vCount := 0
	Loop
	{
		if (vNum > vRange2)
			break
		JEE_TemperatureToRGB(vNum, vColR, vColG, vColB)
		vColRGB := Format("0xFF{:02X}{:02X}{:02X}", vColR, vColG, vColB)
		vPosX := A_Index-1
		Loop, % vImgH
			Gdip_SetPixel(pBitmap, vPosX, A_Index-1, vColRGB)
		vCount++
		vNum += vInc
	}
	if !(vCount = vImgW)
		MsgBox, % "warning: image width miscalculated: " vCount " " vImgW
	vPath = %A_Desktop%\z temperature %vInc% %vRange1%-%vRange2%.png
	if !(FileExist, vPath)
		Gdip_SaveBitmapToFile(pBitmap, vPath, 100)
	Gdip_DisposeImage(pBitmap)
}
Gdip_Shutdown(pToken)
Run, % vPath
;MsgBox, % "done"
return

;==================================================

;[based on:]
;How to Convert Temperature (K) to RGB: Algorithm and Sample Code – Tanner Helland (dot) com
;http://www.tannerhelland.com/4435/convert-temperature-rgb-algorithm-code/

;[similar to SetColorTemperature function:]
;Class Monitor (Brightness, ColorTemperature) - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=7854
;Class_Monitor/Class_Monitor.ahk at master · jNizM/Class_Monitor · GitHub
;https://github.com/jNizM/Class_Monitor/blob/master/src/Class_Monitor.ahk

;temperature in Kelvins
JEE_TemperatureToRGB(vK, ByRef vRed, ByRef vGreen, ByRef vBlue)
{
	vK := Floor(vK / 100)
	if (vK <= 66)
		vRed := 255
	else
	{
		vRed := 329.698727446 * ((vK - 60) ** -0.1332047592)
		(vRed < 0) && vRed := 0
		(vRed > 255) && vRed := 255
	}

	if (vK <= 66)
		vGreen := 99.4708025861 * Ln(vK) - 161.1195681661
	else
		vGreen := 288.1221695283 * ((vK - 60) ** -0.0755148492)
	(vGreen < 0) && vGreen := 0
	(vGreen > 255) && vGreen := 255

	if (vK >= 66)
		vBlue := 255
	else if (vK <= 19)
		vBlue := 0
	else
	{
		vBlue := 138.5177312231 * Ln(vK - 10) - 305.0447927307
		(vBlue < 0) && vBlue := 0
		(vBlue > 255) && vBlue := 255
	}
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 149 guests