contrib/colorpicker.rb (1673B) - raw
1 require 'ncursesw'
2
3 Ncurses.initscr
4 Ncurses.noecho
5 Ncurses.cbreak
6 Ncurses.start_color
7
8 Ncurses.curs_set 0
9 Ncurses.move 0, 0
10 Ncurses.clear
11 Ncurses.refresh
12 cc = Ncurses.COLORS
13
14 Ncurses::keypad(Ncurses::stdscr, 1)
15 Ncurses::mousemask(Ncurses::ALL_MOUSE_EVENTS | Ncurses::REPORT_MOUSE_POSITION, [])
16
17 fail "color count is #{cc}, expected 256" unless cc == 256
18
19 1.upto(255) do |c|
20 Ncurses.init_pair(c, 0, c)
21 end
22
23 def cell y, x, c
24 @map[[y,x]] = c
25 Ncurses.attron(Ncurses.COLOR_PAIR(c))
26 Ncurses.mvaddstr(y, x, " ")
27 Ncurses.attroff(Ncurses.COLOR_PAIR(c))
28 end
29
30 def handle_click y, x
31 c = @map[[y,x]] or return
32 name = case c
33 when 0...16
34 c.to_s
35 when 16...232
36 'c' + (c-16).to_s(6).rjust(3,'0')
37 when 232...256
38 'g' + (c-232).to_s
39 end
40
41 Ncurses.mvaddstr 11, 0, "#{name} "
42
43 Ncurses.attron(Ncurses.COLOR_PAIR(c))
44 10.times do |i|
45 20.times do |j|
46 y = 13 + i
47 x = j
48 Ncurses.mvaddstr(y, x, " ")
49 end
50 end
51 Ncurses.attroff(Ncurses.COLOR_PAIR(c))
52 end
53
54 @map = {}
55 @fg = @bg = 0
56
57 begin
58 16.times do |i|
59 cell 0, i, i
60 end
61
62 6.times do |i|
63 6.times do |j|
64 6.times do |k|
65 c = 16 + 6*6*i + 6*j + k
66 y = 2 + j
67 x = 7*i + k
68 cell y, x, c
69 end
70 end
71 end
72
73 16.times do |i|
74 c = 16 + 6*6*6 + i
75 cell 9, i, c
76 end
77
78 handle_click 0, 0
79 Ncurses.refresh
80
81 while (c = Ncurses.getch)
82 case c
83 when 113 #q
84 break
85 when Ncurses::KEY_MOUSE
86 mev = Ncurses::MEVENT.new
87 Ncurses.getmouse(mev)
88 case(mev.bstate)
89 when Ncurses::BUTTON1_CLICKED
90 handle_click mev.y, mev.x
91 end
92 end
93 Ncurses.refresh
94 end
95
96 ensure
97 Ncurses.endwin
98 end