/* * Get_Screen_Color.bsh: A small text field that shows RGB or * Hexadecimal color value for the current mouse coordinate. * * USAGE: * * 1. Press UP or DOWN to toggle RGB/Hexadecimal values * 2. Press ENTER to copy the current value to the clipboard and stop. * 3. Press ESCAPE to stop without copying. * * IMPORTANT: Requires JRE 1.5 or later * * version 1.0 Sep 3, 2005 * @author Delvin Johnson, delvinj@gmail.com */ void openWin(View v) throws AWTException { int mode = 0; // 0=RGB, 1=HEX Robot robbie = new Robot(); JTextField b = new JTextField("", 14); b.setFont(new Font("DialogInput", Font.PLAIN, 12)); b.setHorizontalAlignment(JTextField.CENTER); b.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createLineBorder(Color.black, 1), BorderFactory.createEmptyBorder(4,4,4,4))); b.setBackground(new Color(0xFFFFD2)); b.setCaretColor(b.getBackground()); JWindow win = new JWindow(v); win.setAlwaysOnTop(true); win.getContentPane().add(b); win.pack(); Color lastColor; ActionListener al = new ActionListener() { public void actionPerformed(ActionEvent e) { if(!win.isVisible()) win.setVisible(true); Point p = MouseInfo.getPointerInfo().getLocation(); win.setLocation(p.x+4, p.y+22); Color c = robbie.getPixelColor(p.x, p.y); if(!c.equals(lastColor)) { String txt; switch(mode) { case 1 : txt = toHexString(c); break; case 0 : default : txt = toRGBString(c); } b.setText(txt); } lastColor = c; } }; javax.swing.Timer timer = new javax.swing.Timer(100, al); public void keyReleased(KeyEvent e) { if(e.getKeyCode() == KeyEvent.VK_ESCAPE) { timer.stop(); win.setVisible(false); win.dispose(); } else if(e.getKeyCode() == KeyEvent.VK_ENTER) { timer.stop(); Registers.setRegister('$', b.getText()); win.setVisible(false); win.dispose(); } else if(e.getKeyCode() == KeyEvent.VK_UP) { if(mode < 1) mode++; else mode = 0; lastColor = null; } else if(e.getKeyCode() == KeyEvent.VK_DOWN) { if(mode > 0) mode--; else mode = 1; lastColor = null; } } public void keyPressed(KeyEvent e) { } public void keyTyped(KeyEvent e) { } public void focusGained(FocusEvent e) { } public void focusLost(FocusEvent e) { timer.stop(); win.setVisible(false); win.dispose(); } b.addFocusListener(this); b.addKeyListener(this); timer.start(); b.requestFocus(); } String toHexString(Color c) { StringBuffer sb = new StringBuffer("0x"); int[] rgb = new int[] { c.getRed(), c.getGreen(), c.getBlue() }; for(int j=0; j < rgb.length; j++) { if(rgb[j] <= 15) sb.append("0"); sb.append(Integer.toHexString(rgb[j]).toUpperCase()); } return sb.toString(); } String toRGBString(Color c) { return c.getRed() + ", " + c.getGreen() + ", " + c.getBlue(); } try { openWin(jEdit.getActiveView()); } catch(AWTException x) { Macros.message(view, x.toString()); }