/**
  * Return the "distance" between two colors.
  *
  * @param color1 First color.
  * @param color2 Second color.
  * @return Distance between colors.
  */
 public static double colorDistance(Color color1, Color color2) {
   float rgb1[] = new float[3];
   float rgb2[] = new float[3];
   color1.getColorComponents(rgb1);
   color2.getColorComponents(rgb2);
   return ColorUtil.colorDistance(rgb1[0], rgb1[1], rgb1[2], rgb2[0], rgb2[1], rgb2[2]);
 }
  /**
   * Create string representation of a fop-rgb-icc (or fop-rgb-named-color) function call from the
   * given color.
   *
   * @param color the color to turn into a function call
   * @return the string representing the internal fop-rgb-icc() or fop-rgb-named-color() function
   *     call
   */
  private static String toFunctionCall(ColorWithAlternatives color) {
    ColorSpace cs = color.getColorSpace();
    if (cs.isCS_sRGB() && !color.hasAlternativeColors()) {
      return toRGBFunctionCall(color);
    }
    if (cs instanceof CIELabColorSpace) {
      return toCIELabFunctionCall(color);
    }

    Color specColor = color;
    if (color.hasAlternativeColors()) {
      Color alt = color.getAlternativeColors()[0];
      if (ColorSpaces.isDeviceColorSpace(alt.getColorSpace())) {
        cs = alt.getColorSpace();
        specColor = alt;
      }
    }
    ColorSpaceOrigin origin = ColorSpaces.getColorSpaceOrigin(cs);
    String functionName;

    Color fallbackColor = getsRGBFallback(color);
    float[] rgb = fallbackColor.getColorComponents(null);
    assert rgb.length == 3;
    StringBuffer sb = new StringBuffer(40);
    sb.append("(");
    sb.append(rgb[0]).append(",");
    sb.append(rgb[1]).append(",");
    sb.append(rgb[2]).append(",");
    String profileName = origin.getProfileName();
    sb.append(profileName).append(",");
    if (origin.getProfileURI() != null) {
      sb.append("\"").append(origin.getProfileURI()).append("\"");
    }

    if (cs instanceof NamedColorSpace) {
      NamedColorSpace ncs = (NamedColorSpace) cs;
      if (SEPARATION_PSEUDO_PROFILE.equalsIgnoreCase(profileName)) {
        functionName = "fop-rgb-icc";
      } else {
        functionName = "fop-rgb-named-color";
      }
      sb.append(",").append(ncs.getColorName());
    } else {
      functionName = "fop-rgb-icc";
      float[] colorComponents = specColor.getColorComponents(null);
      for (float colorComponent : colorComponents) {
        sb.append(",");
        sb.append(colorComponent);
      }
    }
    sb.append(")");
    return functionName + sb.toString();
  }
  /**
   * Shades a color; darker with distance.
   *
   * @param originalColor the original color
   * @param z the z position of the color
   * @return the shaded color
   */
  public static Color shadeColor(Color originalColor, double z) {
    originalColor.getColorComponents(colorComponents);
    backgroundColor.getColorComponents(bkgColorComponents);

    double percentOriginal = (z / 20 + 0.8);
    double percentBackground = 1 - percentOriginal;
    double x;
    for (int i = 0; i < 3; i++) {
      x = colorComponents[i] * percentOriginal + bkgColorComponents[i] * percentBackground;
      colorComponents[i] = (float) (x > 1 ? 1 : x < 0 ? 0 : x);
    }
    return new Color(colorComponents[0], colorComponents[1], colorComponents[2]);
  }
 /**
  * Set the non stroking color, specified as RGB.
  *
  * @param color The color to set.
  * @throws IOException If an IO error occurs while writing to the stream.
  */
 public void setNonStrokingColor(Color color) throws IOException {
   ColorSpace colorSpace = color.getColorSpace();
   if (colorSpace.getType() == ColorSpace.TYPE_RGB) {
     setNonStrokingColor(color.getRed(), color.getGreen(), color.getBlue());
   } else if (colorSpace.getType() == ColorSpace.TYPE_GRAY) {
     color.getColorComponents(colorComponents);
     setNonStrokingColor(colorComponents[0]);
   } else if (colorSpace.getType() == ColorSpace.TYPE_CMYK) {
     color.getColorComponents(colorComponents);
     setNonStrokingColor(
         colorComponents[0], colorComponents[2], colorComponents[2], colorComponents[3]);
   } else {
     throw new IOException("Error: unknown colorspace:" + colorSpace);
   }
 }
  /**
   * Blend two colors.
   *
   * @param color1 First color to blend.
   * @param color2 Second color to blend.
   * @param ratio Blend ratio. 0.5 will give even blend, 1.0 will return color1, 0.0 will return
   *     color2 and so on.
   * @return Blended color.
   */
  public static Color blend(Color color1, Color color2, double ratio) {
    float r = (float) ratio;
    float ir = (float) 1.0 - r;

    float rgb1[] = new float[3];
    float rgb2[] = new float[3];

    color1.getColorComponents(rgb1);
    color2.getColorComponents(rgb2);

    Color color =
        new Color(
            rgb1[0] * r + rgb2[0] * ir, rgb1[1] * r + rgb2[1] * ir, rgb1[2] * r + rgb2[2] * ir);

    return color;
  }
Exemple #6
0
 /**
  * Set the fog color
  *
  * @param color the fog color
  */
 public void setColor(Color color) {
   if (color == null) {
     String msg = Logging.getMessage("nullValue.ColorIsNull");
     Logging.logger().severe(msg);
     throw new IllegalArgumentException(msg);
   }
   color.getColorComponents(this.fogColor);
 }
Exemple #7
0
 @Override
 public ExpressionResult getMember(int i) throws ExecutionException {
   float[] comp = result.getColorComponents(null);
   try {
     return new FloatResult(comp[i]);
   } catch (IndexOutOfBoundsException e) {
     throw new ExecutionException("Index out of bounds", null);
   }
 }
 /**
  * Creates a re-parsable string representation of the given color.
  *
  * <p>First, the color will be converted into the sRGB colorspace. It will then be printed as
  * #rrggbb, or as #rrrggbbaa if an alpha value is present.
  *
  * @param color the color to represent.
  * @return a re-parsable string representadion.
  */
 public static String colorToString(Color color) {
   ColorSpace cs = color.getColorSpace();
   if (color instanceof ColorWithAlternatives) {
     return toFunctionCall((ColorWithAlternatives) color);
   } else if (cs != null && cs.getType() == ColorSpace.TYPE_CMYK) {
     StringBuffer sbuf = new StringBuffer(24);
     float[] cmyk = color.getColorComponents(null);
     sbuf.append("cmyk(")
         .append(cmyk[0])
         .append(",")
         .append(cmyk[1])
         .append(",")
         .append(cmyk[2])
         .append(",")
         .append(cmyk[3])
         .append(")");
     return sbuf.toString();
   } else {
     return toRGBFunctionCall(color);
   }
 }
Exemple #9
0
 @Override
 public void setColor(Color color) {
   float[] colors = color.getColorComponents(null);
   this.color = new RGBColor(colors[0], colors[1], colors[2]);
 }
 public static Color relativelyTransparent(Color original, float alpha) {
   ColorSpace srbg = ICC_ColorSpace.getInstance(ColorSpace.CS_sRGB);
   double originalAlpha = 1.0 * original.getAlpha() / 255;
   alpha *= originalAlpha;
   return new Color(srbg, original.getColorComponents(null), alpha);
 }
  public int getColorComp(Color c) {
    float[] compArray = new float[4];
    c.getColorComponents(compArray);

    return (int) (compArray[mode.ordinal()] * 255);
  }
 public Color getColor(float f) {
   float[] a = c.getColorComponents(new float[4]);
   a[3] = 1.0f;
   return new Color(get(a[0], f), get(a[1], f), get(a[2], f), a[3]);
 }