From 30f2f74d59b5f65a2be41a917404e4bdfa963290 Mon Sep 17 00:00:00 2001 From: sidorares Date: Mon, 25 Jul 2011 19:50:51 +1000 Subject: [PATCH] AllocColor request --- lib/x11/corereqs.js | 14 +++++++++++++ test/alloccolor.js | 48 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 test/alloccolor.js diff --git a/lib/x11/corereqs.js b/lib/x11/corereqs.js index 2840903..72e93e7 100644 --- a/lib/x11/corereqs.js +++ b/lib/x11/corereqs.js @@ -339,6 +339,20 @@ module.exports = { return [format, args]; } ], + + AllocColor: [ + [ 'CxSLSSSxx', [84, 4] ], // params: colormap, red, green, blue + + function(buf) { + var res = buf.unpack('SSSxL'); + var color = {}; + color.red = res[0]; + color.blue = res[1]; + color.green = res[2]; + color.pixel = res[3]; + return color; + } + ], QueryExtension: [ function(name) { diff --git a/test/alloccolor.js b/test/alloccolor.js new file mode 100644 index 0000000..0e9afb1 --- /dev/null +++ b/test/alloccolor.js @@ -0,0 +1,48 @@ +var x11 = require('../lib/x11'); + +var xclient = x11.createClient(); +var Exposure = x11.eventMask.Exposure; +var PointerMotion = x11.eventMask.PointerMotion; +var pts = []; + +xclient.on('connect', function(display) { + var X = this; + var root = display.screen[0].root; + var white = display.screen[0].white_pixel; + var black = display.screen[0].black_pixel; + + var wid = X.AllocID(); + X.CreateWindow( + wid, root, + 10, 10, 400, 300, + 1, 1, 0, + { + backgroundPixel: white, eventMask: Exposure|PointerMotion + } + ); + X.MapWindow(wid); + + var gc = X.AllocID(); + X.AllocColor(display.screen[0].default_colormap, 0xffff, 0, 0, function(redcolor) { + // todo: it is possible for PolyLine to be called before CreateGC! + console.log(redcolor); + X.CreateGC(gc, wid, { foreground: redcolor.pixel, background: white } ); + }); + + X.on('event', function(ev) { + if (ev.type == 12) + { + if (pts.length > 4) + X.PolyLine(0, wid, gc, pts); + } else if (ev.type == 6) { + pts.push(ev.x); + pts.push(ev.y); + if (pts.length > 4) + X.PolyLine(0, wid, gc, pts.slice(-4)); + } + }); + + X.on('error', function(e) { + console.log(e); + }); +});