Skip to content

Commit

Permalink
feat : add transparency in filled or stroke objects
Browse files Browse the repository at this point in the history
  • Loading branch information
sixdouglas committed Oct 17, 2019
1 parent 362a7fe commit ea292a5
Show file tree
Hide file tree
Showing 9 changed files with 462 additions and 80 deletions.
39 changes: 34 additions & 5 deletions openpdf/src/main/java/com/lowagie/text/pdf/CMYKColor.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,19 @@ public class CMYKColor extends ExtendedColor {
* @param intBlack
*/
public CMYKColor(int intCyan, int intMagenta, int intYellow, int intBlack) {
this(intCyan / 255f, intMagenta / 255f, intYellow / 255f, intBlack / 255f);
this(normalize(intCyan) / MAX_INT_COLOR_VALUE, normalize(intMagenta) / MAX_INT_COLOR_VALUE, normalize(intYellow) / MAX_INT_COLOR_VALUE, normalize(intBlack) / MAX_INT_COLOR_VALUE);
}

/**
* Constructs a CMYK Color based on 4 color values (values are integers from 0 to 255).
* @param intCyan
* @param intMagenta
* @param intYellow
* @param intBlack
* @param intAlpha
*/
public CMYKColor(int intCyan, int intMagenta, int intYellow, int intBlack, int intAlpha) {
this(normalize(intCyan) / MAX_INT_COLOR_VALUE, normalize(intMagenta) / MAX_INT_COLOR_VALUE, normalize(intYellow) / MAX_INT_COLOR_VALUE, normalize(intBlack) / MAX_INT_COLOR_VALUE, normalize(intAlpha) / MAX_INT_COLOR_VALUE);
}

/**
Expand All @@ -80,13 +92,25 @@ public CMYKColor(int intCyan, int intMagenta, int intYellow, int intBlack) {
* @param floatBlack
*/
public CMYKColor(float floatCyan, float floatMagenta, float floatYellow, float floatBlack) {
super(TYPE_CMYK, 1f - floatCyan - floatBlack, 1f - floatMagenta - floatBlack, 1f - floatYellow - floatBlack);
this(floatCyan, floatMagenta, floatYellow, floatBlack, MAX_FLOAT_COLOR_VALUE);
}

/**
* Construct a CMYK Color.
* @param floatCyan
* @param floatMagenta
* @param floatYellow
* @param floatBlack
* @param floatAlpha
*/
public CMYKColor(float floatCyan, float floatMagenta, float floatYellow, float floatBlack, float floatAlpha) {
super(TYPE_CMYK, MAX_FLOAT_COLOR_VALUE - normalize(floatCyan) - normalize(floatBlack), MAX_FLOAT_COLOR_VALUE - normalize(floatMagenta) - normalize(floatBlack), MAX_FLOAT_COLOR_VALUE - normalize(floatYellow) - normalize(floatBlack), normalize(floatAlpha));
cyan = normalize(floatCyan);
magenta = normalize(floatMagenta);
yellow = normalize(floatYellow);
black = normalize(floatBlack);
}

/**
* @return the cyan value
*/
Expand Down Expand Up @@ -119,11 +143,16 @@ public boolean equals(Object obj) {
if (!(obj instanceof CMYKColor))
return false;
CMYKColor c2 = (CMYKColor)obj;
return (cyan == c2.cyan && magenta == c2.magenta && yellow == c2.yellow && black == c2.black);
return (cyan == c2.cyan && magenta == c2.magenta && yellow == c2.yellow && black == c2.black && getAlpha() == c2.getAlpha());
}

public int hashCode() {
return Float.floatToIntBits(cyan) ^ Float.floatToIntBits(magenta) ^ Float.floatToIntBits(yellow) ^ Float.floatToIntBits(black);
return Float.floatToIntBits(cyan)
^ Float.floatToIntBits(magenta)
^ Float.floatToIntBits(yellow)
^ Float.floatToIntBits(black)
^ Float.floatToIntBits(getAlpha())
;
}

}
35 changes: 30 additions & 5 deletions openpdf/src/main/java/com/lowagie/text/pdf/ExtendedColor.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,13 @@ public abstract class ExtendedColor extends Color{
public static final int TYPE_PATTERN = 4;
/** a type of extended color. */
public static final int TYPE_SHADING = 5;

/** the max int color value (255) expressed in int */
public static final int MAX_COLOR_VALUE = 0xFF;
/** the max int color value (255) expressed in float */
public static final float MAX_INT_COLOR_VALUE = 0xFF;
/** the max float color value (1) expressed in float */
public static final float MAX_FLOAT_COLOR_VALUE = 0x1;

protected int type;

/**
Expand All @@ -89,10 +95,21 @@ public ExtendedColor(int type) {
* @param blue
*/
public ExtendedColor(int type, float red, float green, float blue) {
super(normalize(red), normalize(green), normalize(blue));
this(type, normalize(red), normalize(green), normalize(blue), MAX_FLOAT_COLOR_VALUE);
}

/**
* Constructs an extended color of a certain type and a certain color.
* @param type
* @param red
* @param green
* @param blue
*/
public ExtendedColor(int type, float red, float green, float blue, float alpha) {
super(normalize(red), normalize(green), normalize(blue), normalize(alpha));
this.type = type;
}

/**
* Gets the type of this color.
* @return one of the types (see constants)
Expand All @@ -113,10 +130,18 @@ public static int getType(Color color) {
}

static final float normalize(float value) {
if (value < 0f)
return 0f;
if (value > MAX_FLOAT_COLOR_VALUE)
return MAX_FLOAT_COLOR_VALUE;
return value;
}

static final int normalize(int value) {
if (value < 0)
return 0;
if (value > 1)
return 1;
if (value > (int)MAX_INT_COLOR_VALUE)
return (int)MAX_INT_COLOR_VALUE;
return value;
}
}
18 changes: 13 additions & 5 deletions openpdf/src/main/java/com/lowagie/text/pdf/GrayColor.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,26 @@ public class GrayColor extends ExtendedColor {

private float gray;

public static final GrayColor GRAYBLACK = new GrayColor(0f);
public static final GrayColor GRAYWHITE = new GrayColor(1f);
public static final GrayColor GRAYBLACK = new GrayColor(0);
public static final GrayColor GRAYWHITE = new GrayColor(MAX_FLOAT_COLOR_VALUE);

public GrayColor(int intGray) {
this(intGray / 255f);
this(normalize(intGray) / MAX_INT_COLOR_VALUE);
}

public GrayColor(float floatGray) {
super(TYPE_GRAY, floatGray, floatGray, floatGray);
this(floatGray, MAX_FLOAT_COLOR_VALUE);
}

public GrayColor(int intGray, int alpha) {
this(normalize(intGray) / MAX_INT_COLOR_VALUE, normalize(alpha) / MAX_INT_COLOR_VALUE);
}

public GrayColor(float floatGray, float alpha) {
super(TYPE_GRAY, normalize(floatGray), normalize(floatGray), normalize(floatGray), normalize(alpha));
gray = normalize(floatGray);
}

public float getGray() {
return gray;
}
Expand Down
Loading

0 comments on commit ea292a5

Please sign in to comment.