Color Index
Posted by poiuy_qwert
Thursday, August 02, 2007
Description: Easily create a Color Index (ctrl+k) controll using the built in dialog icon controll (no dlls).

;;;;;;;;;;;;;;;;;;;
; /colorindex by poiuy_qwert
;;;;;;;;;;;;;;;;;;;
; Easily create a Color Index (ctrl+k) controll using the built in dialog icon controll (no dlls).
;;;;;;;;;;;;;;;;;;;
; /colorindex <dialog> <id> <x y>
;   dialog - The dialog name
;   id     - The ID of the icon controll in the dialog
;   x y    - The x and y coordinates of the icon controll in the dialog
;;;;;;;;;;;;;;;;;;;
;     Usage:
; To use this correctly, first you add an icon controll to your dialog. If your dialog uses the pixels option
; the icon controll will have a width of 168 and height of 39.  For example it should
; look something like:
;   icon 1, 0 0 168 39
; Then in your dialogs init event run the /colorindex command. For the controll above (assuming its in dialog
; Test), the command would look like:
;   /colorindex Test 1 0 0
; If you are using the DBU option (default), use a width of 89 and height of 19, and when using the
; /colorindex command, double the X and Y coordinates. For example:
;   icon 1, 5 10 89 19
; And the command (once again assuming the dialog is Test):
;   /colorindex Test 1 10 20
;
; When the user clicks on one of the colors, the colorindex signal is sent. The parameters of this signal are:
;   <dialog> <id> <color> <rgb>
; Where dialog is the dialog name, id is the ID of the controll, color is which color was selected, and rgb
; is the rgb of that color.
;;;;;;;;;;;;;;;;;;;
;     WARNING:
; - Using DBU will not be perfect!
; - You must have this code in a seperate file from all other on DIALOG events or they will conflict!
; - Because of the above problem, you will have to move the example bellow into a seperate file to run it!
; - There is no error checking, supply valid parameters!
;;;;;;;;;;;;;;;;;;;
;     Example:
;alias citest dialog -md ColorIndex ColorIndex
;dialog ColorIndex {
;  title "Color Index"
;  size -1 -1 168 39
;  option pixels
;  icon 1, 0 0 168 39
;}
;on *:DIALOG:ColorIndex:init:*:colorindex $dname 1 0 0
;on *:SIGNAL:colorindex:if ($1 == ColorIndex) echo -a Color Index - User selected color $3 which has an RGB of $4 ( $+ $rgb($4) $+ )
;;;;;;;;;;;;;;;;;;;
alias colorindex {
  var %f colorindex.bmp,%w @colorindex,%t @compress,%l = drawline -rn %w,%r drawrect -rnf,%c drawcopy -n,%a 1,%z $regsubex($str(!-,16),/!/g,$color($calc(n-1)))
  if (!$exists(%f)) || (!$pic(%f)) || ($pic(%f).w $pic(%f).h != 168 39) || (!$hget(colorindex)) || ($hget(colorindex,colors) != %z) {
    hadd -m colorindex colors %z
    window -pdfhn %w 0 0 168 39
    %r %w $rgb(face) 1 0 0 168 39
    %l $rgb(shadow) 1 1 17 1 1 19 1
    %l 0 1 2 16 2 2 18 2
    %l $rgb(hilight) 1 2 17 19 17 19 0
    while (%a < 17) {
      %c %w 1 1 19 17 %w $calc(21*(%a % 9)+1) $iif(%a > 8,21,1)
      var %a = %a - 1,%x = $calc(21*(%a % 8)+3),%y = $iif(%a > 7,23,3)
      %r %w $color(%a) 1 %x %y 15 13
      drawtext -rn %w 0 System 8 $calc(8+%x -$width(%a,System,8)/2) $calc(6+%y -$height(%a,System,8)/2) %a
      inc %a 2
    }
    .drawsave %w %f
    window -c %w
  }
  did -g $1-2 %f
  hadd -m colorindex $+($1,.,$2) $3-
}
on *:DIALOG:*:*:*:{
  if ($devent == init) || ($devent == close) {
    while ($hfind(colorindex,$+(/,$regsubex($dname,/([$^/.[]()|?*+])/g,\t),.d+/),1,r)) hdel colorindex $v1
  }
  else {
    tokenize 32 $hget(colorindex,$+($dname,.,$did))
    if ($1-) && ($devent == sclick) && ($calc(($mouse.x-1-$1) % 21) isnum 1-19) && ($calc(($mouse.y-1-$2) % 20) isnum 1-17) {
      var %c $or($calc(($mouse.x-1)/21+$iif($mouse.y > 21,8)))
      .signal -n colorindex $dname $did %c $color(%c)
    }
  }
}
Submit a comment
Oops! You need to login or register before you can post a comment!

ebaum's world