AutoHotkey Community

It is currently May 27th, 2012, 9:01 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: August 8th, 2006, 4:36 pm 
Offline

Joined: March 17th, 2005, 10:25 am
Posts: 19
Location: 10247 Berlin
I work with CSS files. Sometimes I want a HEX color slightly darker or brighter than an existing color. From your text editor, select something like 4E80B3 and press Ctrl+Alt+Shift+NumpadAdd (brighter) or Ctrl+Alt+Shift+NumpadSub (darker). The 6 characters will be replaced with the new color.

Code:
^!+NumpadAdd::
  clipboard = ;
  SendInput ^c
  ClipWait
  StringRight, rgb, clipboard, 6
  StringRight, bb, rgb, 2
  StringLeft, rr, rgb, 2
  StringMid, gg, rgb, 3, 2
  rr := "0x" . rr
  gg := "0x" . gg
  bb := "0x" . bb
  SetFormat, integer, h
  rr := Round(rr * 1.1)
  gg := Round(gg * 1.1)
  bb := Round(bb * 1.1)
  StringRight, rr, rr, 2
  StringRight, gg, gg, 2
  StringRight, bb, bb, 2
  SendInput %rr%%gg%%bb%
return
^!+NumpadAdd::
  clipboard = ;
  SendInput ^c
  ClipWait
  StringRight, rgb, clipboard, 6
  StringRight, bb, rgb, 2
  StringLeft, rr, rgb, 2
  StringMid, gg, rgb, 3, 2
  rr := "0x" . rr
  gg := "0x" . gg
  bb := "0x" . bb
  SetFormat, integer, h
  rr := Round(rr * 0.9)
  gg := Round(gg * 0.9)
  bb := Round(bb * 0.9)
  StringRight, rr, rr, 2
  StringRight, gg, gg, 2
  StringRight, bb, bb, 2
  SendInput %rr%%gg%%bb%
return


Can be improved:
* Does not check limits, so it messes up if you try to brighten FFFFFF or darken 000000.
* Would be great if the selection was not lost after pressing the keys, this way you could press them several times.
* I multiply by 1.1 or 0.9 to change the brighness. This is not very good, because it makes impossible to brighten black. Better would be use +10 and -10 for red, and adjust green and blue so the proportion to red is kept.
* A better way would be to show a brightness slider to adjust the color with a GUI.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 8th, 2006, 8:33 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
This is useful, a good idea. Here is a little mod, which keeps the selection, saturates at 00 or FF, does not have unchangeable values, and always produces two-digit hex values (00...0F). Experiment with the increment/decrement functions.
Code:
SetFormat Integer, Hex
#UseHook

^NumpadAdd::
^NumpadSub::
  ClipBoard =
  SendInput ^c
  ClipWait 2
  IfEqual ErrorLevel,1, Return
  StringLeft  rr, ClipBoard, 2
  StringMid   gg, ClipBoard, 3, 2
  StringRight bb, ClipBoard, 2
  If A_ThisHotKey = ^NumpadAdd
     SendInput % Inc(rr) Inc(gg) Inc(bb) "+{Left 6}"
  Else
     SendInput % Dec(rr) Dec(gg) Dec(bb) "+{Left 6}"
Return

Inc(x) {
  x := Round(Abs("0x" x) * 1.1) + 0x101
  IfGreater x, 0x1FF, Return "ff"
  StringRight x, x, 2
  Return x
}
Dec(x) {
  x := Round(Abs("0x" x) * 0.9) + 0xff
  IfLess x, 0x100, Return "00"
  StringRight x, x, 2
  Return x
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 10th, 2006, 3:58 pm 
Offline

Joined: March 17th, 2005, 10:25 am
Posts: 19
Location: 10247 Berlin
Wow thanks! That's much more clean and organized.

These formulas should compensate the lack of change with dark colors:

Code:
x * 1.05 + (1-(x/255))*10
x * 0.95 - (1-(x/255))*10


(gives increments close to 10 units up or down)

Code:
SetFormat Integer, Hex
#UseHook

^NumpadAdd::
^NumpadSub::
  ClipBoard =
  SendInput ^c
  ClipWait 2
  IfEqual ErrorLevel,1, Return
  StringLeft  rr, ClipBoard, 2
  StringMid   gg, ClipBoard, 3, 2
  StringRight bb, ClipBoard, 2
  If A_ThisHotKey = ^NumpadAdd
     SendInput % Inc(rr) Inc(gg) Inc(bb) "+{Left 6}"
  Else
     SendInput % Dec(rr) Dec(gg) Dec(bb) "+{Left 6}"
Return

Inc(x) {
  x := Round(Abs("0x" x) * 1.05 + (1 - (Abs("0x" x) / 255)) * 10) + 0x101
  IfGreater x, 0x1FF, Return "ff"
  StringRight x, x, 2
  Return x
}
Dec(x) {
  x := Round(Abs("0x" x) * 0.95 - (1 - (Abs("0x" x) / 255)) * 10) + 0x101
  IfLess x, 0x100, Return "00"
  StringRight x, x, 2
  Return x
}


So now it works even with 000000 and FFFFFF.

If someone wants to adjust the increment: the *10 is responsible for the increment when we have dark colors, and the 1.05 and 0.95 for the bright colors. For a smaller step you can try using *5, 1.03 and 0.97 for example.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 9 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group