1 /*
   2  *
   3  * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
   4  *
   5  * Redistribution and use in source and binary forms, with or without
   6  * modification, are permitted provided that the following conditions
   7  * are met:
   8  *
   9  *   - Redistributions of source code must retain the above copyright
  10  *     notice, this list of conditions and the following disclaimer.
  11  *
  12  *   - Redistributions in binary form must reproduce the above copyright
  13  *     notice, this list of conditions and the following disclaimer in the
  14  *     documentation and/or other materials provided with the distribution.
  15  *
  16  *   - Neither the name of Oracle nor the names of its
  17  *     contributors may be used to endorse or promote products derived
  18  *     from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  21  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 package java2d.demos.Colors;
  33 
  34 
  35 import static java.awt.Color.black;
  36 import static java.awt.Color.blue;
  37 import static java.awt.Color.cyan;
  38 import static java.awt.Color.green;
  39 import static java.awt.Color.magenta;
  40 import static java.awt.Color.orange;
  41 import static java.awt.Color.pink;
  42 import static java.awt.Color.red;
  43 import static java.awt.Color.white;
  44 import static java.awt.Color.yellow;
  45 import java.awt.Color;
  46 import java.awt.Font;
  47 import java.awt.Graphics2D;
  48 import java.awt.Image;
  49 import java.awt.RenderingHints;
  50 import java.awt.Shape;
  51 import java.awt.color.ColorSpace;
  52 import java.awt.font.FontRenderContext;
  53 import java.awt.font.TextLayout;
  54 import java.awt.geom.Rectangle2D;
  55 import java.awt.image.BufferedImage;
  56 import java.awt.image.ColorConvertOp;
  57 import java2d.Surface;
  58 
  59 
  60 /**
  61  * ColorConvertOp a ColorSpace.TYPE_RGB BufferedImage to a ColorSpace.CS_GRAY
  62  * BufferedImage.
  63  */
  64 @SuppressWarnings("serial")
  65 public class ColorConvert extends Surface {
  66 
  67     private static Image img;
  68     private static Color[] colors = { red, pink, orange,
  69         yellow, green, magenta, cyan, blue };
  70 
  71     public ColorConvert() {
  72         setBackground(white);
  73         img = getImage("clouds.jpg");
  74     }
  75 
  76     @Override
  77     public void render(int w, int h, Graphics2D g2) {
  78 
  79         int iw = img.getWidth(this);
  80         int ih = img.getHeight(this);
  81         FontRenderContext frc = g2.getFontRenderContext();
  82         Font font = g2.getFont();
  83         g2.setColor(black);
  84         TextLayout tl = new TextLayout("ColorConvertOp RGB->GRAY", font, frc);
  85         tl.draw(g2, (float) (w / 2 - tl.getBounds().getWidth() / 2),
  86                 tl.getAscent() + tl.getLeading());
  87 
  88         BufferedImage srcImg =
  89                 new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
  90         Graphics2D srcG = srcImg.createGraphics();
  91         RenderingHints rhs = g2.getRenderingHints();
  92         srcG.setRenderingHints(rhs);
  93         srcG.drawImage(img, 0, 0, null);
  94 
  95         String s = "OpenJDK";
  96         Font f = new Font(Font.SERIF, Font.BOLD, iw / 6);
  97         tl = new TextLayout(s, f, frc);
  98         Rectangle2D tlb = tl.getBounds();
  99         char[] chars = s.toCharArray();
 100         float charWidth = 0.0f;
 101         int rw = iw / chars.length;
 102         int rh = ih / chars.length;
 103         for (int i = 0; i < chars.length; i++) {
 104             tl = new TextLayout(String.valueOf(chars[i]), f, frc);
 105             Shape shape = tl.getOutline(null);
 106             srcG.setColor(colors[i % colors.length]);
 107             tl.draw(srcG, (float) (iw / 2 - tlb.getWidth() / 2 + charWidth),
 108                     (float) (ih / 2 + tlb.getHeight() / 2));
 109             charWidth += (float) shape.getBounds().getWidth();
 110             srcG.fillRect(i * rw, ih - rh, rw, rh);
 111             srcG.setColor(colors[colors.length - 1 - i % colors.length]);
 112             srcG.fillRect(i * rw, 0, rw, rh);
 113         }
 114 
 115         ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
 116         ColorConvertOp theOp = new ColorConvertOp(cs, rhs);
 117 
 118         BufferedImage dstImg =
 119                 new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
 120         theOp.filter(srcImg, dstImg);
 121 
 122         g2.drawImage(srcImg, 10, 20, w / 2 - 20, h - 30, null);
 123         g2.drawImage(dstImg, w / 2 + 10, 20, w / 2 - 20, h - 30, null);
 124     }
 125 
 126     public static void main(String[] s) {
 127         createDemoFrame(new ColorConvert());
 128     }
 129 }