[Home] [Downloads] [Search] [Help/forum]

Gammon Software Solutions forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  A Few Possibly Handy Functions - Colour Picker, Conversion, Hex
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

A Few Possibly Handy Functions - Colour Picker, Conversion, Hex

[Reply to this subject]  Reply to this subject   [New subject]  Start a new subject   [Refresh] Refresh page


Posted by Beale   (35 posts)  [Biography] bio
Date Wed 17 Aug 2005 12:37 AM (UTC)  quote  ]
Message
So, in my quest to make a good colourpicker, I ended up with the code shown below. It contains functions:
colourPicker ( string sCommandHead, string sCommandFoot )
fColourList()
hsltorgb(float hue, float saturation, float lightness)
huetorgb(float param1, float param2, float param3)
int2char(int number)
hex(int number)


I figured someone else might find these handy, for something.

colourPicker depends on all of the above functions, and generates a rather large array (40 x 20, with spaces between) of coloured octothorpes (yes, that's what they're called, not "pound signs" or "hashes" :) ) that link to commands containing their colour between sCommandHead and sCommandFoot.

fColourList generates the large array of colours that colourPicker displays/uses.

hsltorgb does exactly what it says on the tin: turns a HSL colour space colour into an RGB one, preceded by a "#" for ease of use. It depends on huetorgb, since that's the form the algorithm I found when I was looking was in. Note that the HSL colour is expected in fractional components, rather than 1-256 ones.

hex turns a decimal number into a hexidecimal one, and depends on int2char.


function colourPicker( sCommandHead, sCommandFoot )
  local iCount = 1
  local arrColourList = fColourList()
  for k, v in pairs(arrColourList) do
    ColourTell(v, "", "#")
    world.Hyperlink(sCommandHead .. v .. sCommandFoot, " ", v, RGBColourToName(GetNoteColourBack()), "", false)
    Tell("")
    iCount = iCount + 1
    if iCount == 41 then iCount = 1 world.Note("") end
  end
  Note("")
end

function fColourList()
  iIndex = 1
  arrOut = {}
  --Black through White - the first line.
  while iIndex<41 do
    arrOut[iIndex] = hsltorgb(0, 0, iIndex/40)
    iIndex = iIndex + 1
  end
  local hue = 0
  local sat = 1
  local light = 0
  --The Rest - increment hue, go from dark to light, repeat.
  while hue<=1 do
    light = 0
    while light<=1 do
      arrOut[iIndex] = hsltorgb(hue, sat, light)
      light = light + (1/40)
      iIndex = iIndex + 1
    end
    hue = hue + (1/20)
  end
  return arrOut
end

function hsltorgb(h, s, l)
	if l<=0.5 then
		m2=l*(s+1)
	else 
		m2=l+s-l*s
	end
	m1=l*2-m2
	r=huetorgb(m1, m2, h+1/3)
	g=huetorgb(m1, m2, h    )
	b=huetorgb(m1, m2, h-1/3)
	r=math.floor(r*255)
	g=math.floor(g*255)
	b=math.floor(b*255)
  
	sOut = "#"
        if r<16 then sOut = sOut .. "0" .. hex(r) else sOut = sOut .. hex(r) end
	if g<16 then sOut = sOut .. "0" .. hex(g) else sOut = sOut .. hex(g) end
	if b<16 then sOut = sOut .. "0" .. hex(b) else sOut = sOut .. hex(b) end
	return sOut
end

function huetorgb(m1, m2, h)
  if h<0 then h=h+1 end
  if h>1 then h=h-1 end
  if h*6<1 then return m1+(m2-m1)*h*6 end
  if h*2<1 then return m2 end
  if h*3<2 then return m1+(m2-m1)*(2/3-h)*6 end
  return m1
end

function int2char(a)
if a >  16 then return nil end
if a == 15 then return 'F' end
if a == 14 then return 'E' end
if a == 13 then return 'D' end
if a == 12 then return 'C' end
if a == 11 then return 'B' end
if a == 10 then return 'A' end
return a
end

function hex(a)
iIn = tonumber(a)
if iIn == 0 then return "0" end
iLoop = 1
sOut=""
while iIn > 0 do
	iChar = math.mod(iIn,16^iLoop)/(16^(iLoop-1))
	--world.Note(iChar)
	iIn = iIn - math.mod(iIn,16^iLoop)
	sOut = int2char(iChar) .. sOut 
	iLoop = iLoop + 1
end
return sOut
end
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


2,064 views.

[Reply to this subject]  Reply to this subject   [New subject]  Start a new subject   [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

[Home]

Written by Nick Gammon - 5K

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( http://www.gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Internet Contents Rating Association (ICRA) - 2K]    [Web site powered by FutureQuest.Net]