private BufferedImage figureOutDrawImage(Sprite[] imgs) { Sprite img; if (dead) { return imgs[5].getBuffer(); } if (Math.abs((int) (vel.y * 100)) > 0 && !piped) { img = imgs[1]; } else if (!(movingRight || movingLeft) && vel.x == 0) img = imgs[0]; else { if (movingRight && vel.x < 0 || movingLeft && vel.x > 0) img = imgs[4]; else { if (rightFoot) img = imgs[3]; else img = imgs[2]; } } if (star && (starTime < 8000 / 15 || System.currentTimeMillis() % 60 > 30)) { int width = img.getBuffer().getWidth(), height = img.getBuffer().getHeight(); Sprite limg = new Sprite(img.getBuffer()); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { Color pixel = limg.getPixel(x, y); if (pixel.getAlpha() != 0) { limg.setPixel( x, y, new Color( 255 - pixel.getRed(), 255 - pixel.getGreen(), 255 - pixel.getBlue(), pixel.getAlpha())); } } } img = limg; } else if (metal) { int width = img.getBuffer().getWidth(), height = img.getBuffer().getHeight(); Sprite limg = new Sprite(img.getBuffer()); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { Color pixel = limg.getPixel(x, y); if (pixel.getAlpha() != 0) { int gray = (pixel.getRed() + pixel.getBlue() + pixel.getGreen()) / 3; limg.setPixel(x, y, new Color(gray, gray, gray, pixel.getAlpha())); } } } img = limg; } if (facingRight) { return img.flipX(); } else { return img.getBuffer(); } }
/** * Sets the selected color of this panel. * * <p>If this panel is in RED, GREEN, or BLUE mode, then this method converts these values to RGB * coordinates and calls <code>setRGB</code>. * * <p>This method may regenerate the graphic if necessary. * * @param h the hue value of the selected color. * @param s the saturation value of the selected color. * @param b the brightness value of the selected color. */ public void setHSB(float h, float s, float b) { if (Float.isInfinite(h) || Float.isNaN(h)) throw new IllegalArgumentException("The hue value (" + h + ") is not a valid number."); // hue is cyclic, so it can be any value: while (h < 0) h++; while (h > 1) h--; if (s < 0 || s > 1) throw new IllegalArgumentException("The saturation value (" + s + ") must be between [0,1]"); if (b < 0 || b > 1) throw new IllegalArgumentException("The brightness value (" + b + ") must be between [0,1]"); if (hue != h || sat != s || bri != b) { if (mode == ColorPicker.HUE || mode == ColorPicker.BRI || mode == ColorPicker.SAT) { float lastHue = hue; float lastBri = bri; float lastSat = sat; hue = h; sat = s; bri = b; if (mode == ColorPicker.HUE) { if (lastHue != hue) { regenerateImage(); } } else if (mode == ColorPicker.SAT) { if (lastSat != sat) { regenerateImage(); } } else if (mode == ColorPicker.BRI) { if (lastBri != bri) { regenerateImage(); } } } else { Color c = new Color(Color.HSBtoRGB(h, s, b)); setRGB(c.getRed(), c.getGreen(), c.getBlue()); return; } Color c = new Color(Color.HSBtoRGB(hue, sat, bri)); red = c.getRed(); green = c.getGreen(); blue = c.getBlue(); regeneratePoint(); repaint(); fireChangeListeners(); } }
public void addPlot(String name, int[] data) { plot_names.add(name); plot_data.add(data); int new_max = getMax(plot_data.size() - 1); if (new_max > MAX_Y) { MAX_Y = new_max; } if (data.length > MAX_X) { MAX_X = data.length; } int i = plot_data.size(); Color c = new Color( (i * COLOR_RATIO) % 255, ((i + 2) * COLOR_RATIO) % 255, ((i + 3) * COLOR_RATIO) % 255, 255); plot_colors.add(c); for (int j = 0; j < plot_colors.size(); j++) { Color color = plot_colors.get(j); Color new_color = new Color( color.getRed(), color.getBlue(), color.getGreen(), 127 / plot_colors.size() + 128); plot_colors.set(j, new_color); } }
/** * Compute a contrasting background color to draw the label's outline. * * @param color Label color. * @return A color that contrasts with {@code color}. */ protected Color computeBackgroundColor(Color color) { float[] colorArray = new float[4]; Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), colorArray); if (colorArray[2] > 0.5) return new Color(0, 0, 0, 0.7f); else return new Color(1, 1, 1, 0.7f); }
/** * Draw labels for picking. * * @param dc Current draw context. * @param pickSupport the PickSupport instance to be used. */ protected void doPick(DrawContext dc, PickSupport pickSupport) { GL gl = dc.getGL(); Angle heading = this.rotation; double headingDegrees; if (heading != null) headingDegrees = heading.degrees; else headingDegrees = 0; int x = this.screenPoint.x; int y = this.screenPoint.y; boolean matrixPushed = false; try { if (headingDegrees != 0) { gl.glPushMatrix(); matrixPushed = true; gl.glTranslated(x, y, 0); gl.glRotated(headingDegrees, 0, 0, 1); gl.glTranslated(-x, -y, 0); } for (int i = 0; i < this.lines.length; i++) { Rectangle2D bounds = this.lineBounds[i]; double width = bounds.getWidth(); double height = bounds.getHeight(); x = this.screenPoint.x; if (this.textAlign.equals(AVKey.CENTER)) x = x - (int) (width / 2.0); else if (this.textAlign.equals(AVKey.RIGHT)) x = x - (int) width; y -= this.lineHeight; Color color = dc.getUniquePickColor(); int colorCode = color.getRGB(); PickedObject po = new PickedObject(colorCode, this.getPickedObject(), this.position, false); pickSupport.addPickableObject(po); // Draw line rectangle gl.glColor3ub((byte) color.getRed(), (byte) color.getGreen(), (byte) color.getBlue()); try { gl.glBegin(GL.GL_POLYGON); gl.glVertex3d(x, y, 0); gl.glVertex3d(x + width - 1, y, 0); gl.glVertex3d(x + width - 1, y + height - 1, 0); gl.glVertex3d(x, y + height - 1, 0); gl.glVertex3d(x, y, 0); } finally { gl.glEnd(); } y -= this.lineSpacing; } } finally { if (matrixPushed) { gl.glPopMatrix(); } } }
public static void main(String[] args) { int x = 500, y = 80; DrawingKit dk = new DrawingKit("Daffodils", 800, 800); BufferedImage pict = dk.loadPicture("daffodils.jpg"); // get pixel value at location (500, 80) int encodedPixelColor = pict.getRGB(x, y); Color pixelColor = new Color(encodedPixelColor); System.out.println(pixelColor); int red = pixelColor.getRed(); int green = pixelColor.getGreen(); int blue = pixelColor.getBlue(); // change the color of the pixel to be pure red red = 255; green = 0; blue = 0; // update the pixel color in picture Color newPixelColor = new Color(red, green, blue); int newRgbvalue = newPixelColor.getRGB(); pict.setRGB(x, y, newRgbvalue); // display the approximate location of the pixel dk.drawPicture(pict, 0, 0); BasicStroke s = new BasicStroke(3); dk.setStroke(s); Ellipse2D.Float e = new Ellipse2D.Float(x - 3, y - 3, 8, 8); dk.draw(e); dk.drawString("(600, 150)", x - 3, y - 5); }
/** * Sets the fillColor and defines the outline color to be a non-transparent version of the fill * color */ public void setColor(Color fillColor) { m_fillColor = fillColor; m_outlineColor = new Color( fillColor.getRed() < 255 ? 0.0f : 1.0f, fillColor.getGreen() < 255 ? 0.0f : 1.0f, fillColor.getBlue() < 255 ? 0.0f : 1.0f); }
void initGraphics(Graphics g, Color textColor, Color defaultColor) { if (smallFont == null) { smallFont = new Font("SansSerif", Font.PLAIN, 9); largeFont = new Font("SansSerif", Font.PLAIN, 12); } if (textColor != null) { labelColor = textColor; if (overlay != null && overlay.getDrawBackgrounds()) bgColor = new Color( 255 - labelColor.getRed(), 255 - labelColor.getGreen(), 255 - labelColor.getBlue()); else bgColor = null; } else { int red = defaultColor.getRed(); int green = defaultColor.getGreen(); int blue = defaultColor.getBlue(); if ((red + green + blue) / 3 < 128) labelColor = Color.white; else labelColor = Color.black; bgColor = defaultColor; } this.defaultColor = defaultColor; g.setColor(defaultColor); }
/* ****************** CHECKLIST ****************************** [x] manipulate every Pixel in the Picture [x] The value of amount MUST control the amount of the effect */ public void changeWhole(double amount) { int yMax = this.getHeight(); int xMax = this.getWidth(); Color p; for (int i = 0; i < xMax - 1; i++) { for (int j = 0; j < yMax - 1; j++) { p = this.getPixel(i, j).getColor(); int R = (int) (p.getRed() * amount); int G = (int) (p.getBlue() * (amount)); int B = (int) (p.getGreen() * (amount)); Color C = new Color(G, B, R); this.getPixel(i, j).setColor((C)); } } }
/** * Render the label interior as a filled rectangle. * * @param dc Current draw context. */ protected void drawInterior(DrawContext dc) { GL gl = dc.getGL(); double width = this.bounds.getWidth(); double height = this.bounds.getHeight(); int x = this.screenPoint.x; int y = this.screenPoint.y; // Adjust x to account for text alignment int xAligned = x; if (AVKey.CENTER.equals(textAlign)) xAligned = x - (int) (width / 2); else if (AVKey.RIGHT.equals(textAlign)) xAligned = x - (int) width; // We draw text top-down, so adjust y to compensate. int yAligned = (int) (y - height); // Apply insets Insets insets = this.getInsets(); xAligned -= insets.left; width = width + insets.left + insets.right; yAligned -= insets.bottom; height = height + insets.bottom + insets.top; if (!dc.isPickingMode()) { // Apply the frame background color and opacity if we're in normal rendering mode. Color color = this.computeBackgroundColor(this.getMaterial().getDiffuse()); gl.glColor4ub( (byte) color.getRed(), (byte) color.getGreen(), (byte) color.getBlue(), (byte) (this.interiorOpacity < 1 ? (int) (this.interiorOpacity * 255 + 0.5) : 255)); } try { // Draw a quad gl.glPushMatrix(); gl.glTranslated(xAligned, yAligned, 0); gl.glScaled(width, height, 1.0); dc.drawUnitQuad(); } finally { gl.glPopMatrix(); } }
protected void setDrawingColor(int ox, int oy, boolean setBackground) { // IJ.log("setDrawingColor: "+setBackground+this); int type = imp.getType(); int[] v = imp.getPixel(ox, oy); switch (type) { case ImagePlus.GRAY8: { if (setBackground) setBackgroundColor(getColor(v[0])); else setForegroundColor(getColor(v[0])); break; } case ImagePlus.GRAY16: case ImagePlus.GRAY32: { double min = imp.getProcessor().getMin(); double max = imp.getProcessor().getMax(); double value = (type == ImagePlus.GRAY32) ? Float.intBitsToFloat(v[0]) : v[0]; int index = (int) (255.0 * ((value - min) / (max - min))); if (index < 0) index = 0; if (index > 255) index = 255; if (setBackground) setBackgroundColor(getColor(index)); else setForegroundColor(getColor(index)); break; } case ImagePlus.COLOR_RGB: case ImagePlus.COLOR_256: { Color c = new Color(v[0], v[1], v[2]); if (setBackground) setBackgroundColor(c); else setForegroundColor(c); break; } } Color c; if (setBackground) c = Toolbar.getBackgroundColor(); else { c = Toolbar.getForegroundColor(); imp.setColor(c); } IJ.showStatus("(" + c.getRed() + ", " + c.getGreen() + ", " + c.getBlue() + ")"); }
// Rendering public void draw(DrawContext dc) { GL gl = dc.getGL(); boolean attribsPushed = false; boolean modelviewPushed = false; boolean projectionPushed = false; try { gl.glPushAttrib( GL.GL_DEPTH_BUFFER_BIT | GL.GL_COLOR_BUFFER_BIT | GL.GL_ENABLE_BIT | GL.GL_TEXTURE_BIT | GL.GL_TRANSFORM_BIT | GL.GL_VIEWPORT_BIT | GL.GL_CURRENT_BIT); attribsPushed = true; gl.glDisable(GL.GL_TEXTURE_2D); // no textures gl.glEnable(GL.GL_BLEND); gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); gl.glDisable(GL.GL_DEPTH_TEST); double width = this.size.width; double height = this.size.height; // Load a parallel projection with xy dimensions (viewportWidth, viewportHeight) // into the GL projection matrix. java.awt.Rectangle viewport = dc.getView().getViewport(); gl.glMatrixMode(javax.media.opengl.GL.GL_PROJECTION); gl.glPushMatrix(); projectionPushed = true; gl.glLoadIdentity(); double maxwh = width > height ? width : height; gl.glOrtho(0d, viewport.width, 0d, viewport.height, -0.6 * maxwh, 0.6 * maxwh); gl.glMatrixMode(GL.GL_MODELVIEW); gl.glPushMatrix(); modelviewPushed = true; gl.glLoadIdentity(); // Scale to a width x height space // located at the proper position on screen double scale = this.computeScale(viewport); Vec4 locationSW = this.computeLocation(viewport, scale); gl.glTranslated(locationSW.x(), locationSW.y(), locationSW.z()); gl.glScaled(scale, scale, 1); // Compute scale size in real world Position referencePosition = dc.getViewportCenterPosition(); if (referencePosition != null) { Vec4 groundTarget = dc.getGlobe().computePointFromPosition(referencePosition); Double distance = dc.getView().getEyePoint().distanceTo3(groundTarget); this.pixelSize = dc.getView().computePixelSizeAtDistance(distance); Double scaleSize = this.pixelSize * width * scale; // meter String unitLabel = "m"; if (this.unit.equals(UNIT_METRIC)) { if (scaleSize > 10000) { scaleSize /= 1000; unitLabel = "Km"; } } else if (this.unit.equals(UNIT_IMPERIAL)) { scaleSize *= 3.280839895; // feet unitLabel = "ft"; if (scaleSize > 5280) { scaleSize /= 5280; unitLabel = "mile(s)"; } } // Rounded division size int pot = (int) Math.floor(Math.log10(scaleSize)); if (!Double.isNaN(pot)) { int digit = Integer.parseInt(String.format("%.0f", scaleSize).substring(0, 1)); double divSize = digit * Math.pow(10, pot); if (digit >= 5) divSize = 5 * Math.pow(10, pot); else if (digit >= 2) divSize = 2 * Math.pow(10, pot); double divWidth = width * divSize / scaleSize; // Draw scale if (!dc.isPickingMode()) { // Set color using current layer opacity Color backColor = this.getBackgroundColor(this.color); float[] colorRGB = backColor.getRGBColorComponents(null); gl.glColor4d( colorRGB[0], colorRGB[1], colorRGB[2], (double) backColor.getAlpha() / 255d * this.getOpacity()); gl.glTranslated((width - divWidth) / 2, 0d, 0d); this.drawScale(dc, divWidth, height); colorRGB = this.color.getRGBColorComponents(null); gl.glColor4d(colorRGB[0], colorRGB[1], colorRGB[2], this.getOpacity()); gl.glTranslated(-1d / scale, 1d / scale, 0d); this.drawScale(dc, divWidth, height); // Draw label String label = String.format("%.0f ", divSize) + unitLabel; gl.glLoadIdentity(); gl.glDisable(GL.GL_CULL_FACE); drawLabel( dc, label, locationSW.add3( new Vec4(divWidth * scale / 2 + (width - divWidth) / 2, height * scale, 0))); } else { // Picking this.pickSupport.clearPickList(); this.pickSupport.beginPicking(dc); // Draw unique color across the map Color color = dc.getUniquePickColor(); int colorCode = color.getRGB(); // Add our object(s) to the pickable list this.pickSupport.addPickableObject(colorCode, this, referencePosition, false); gl.glColor3ub((byte) color.getRed(), (byte) color.getGreen(), (byte) color.getBlue()); gl.glTranslated((width - divWidth) / 2, 0d, 0d); this.drawRectangle(dc, divWidth, height); // Done picking this.pickSupport.endPicking(dc); this.pickSupport.resolvePick(dc, dc.getPickPoint(), this); } } } } finally { if (projectionPushed) { gl.glMatrixMode(GL.GL_PROJECTION); gl.glPopMatrix(); } if (modelviewPushed) { gl.glMatrixMode(GL.GL_MODELVIEW); gl.glPopMatrix(); } if (attribsPushed) gl.glPopAttrib(); } }
static ColorUIResource getColorTercio(Color a, Color b) { return new ColorUIResource( propInt(a.getRed(), b.getRed(), 3), propInt(a.getGreen(), b.getGreen(), 3), propInt(a.getBlue(), b.getBlue(), 3)); }
static Color getColorMedio(Color a, Color b) { return new Color( propInt(a.getRed(), b.getRed(), 2), propInt(a.getGreen(), b.getGreen(), 2), propInt(a.getBlue(), b.getBlue(), 2)); }
static Color getColorAlfa(Color col, int alfa) { return new Color(col.getRed(), col.getGreen(), col.getBlue(), alfa); }
private void setBackgroundColor(Color c) { Toolbar.setBackgroundColor(c); if (Recorder.record) Recorder.record("setBackgroundColor", c.getRed(), c.getGreen(), c.getBlue()); }
// Compute background color for best contrast private Color getBackgroundColor(Color color) { Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), compArray); if (compArray[2] > 0.5) return new Color(0, 0, 0, 0.7f); else return new Color(1, 1, 1, 0.7f); }