Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix PdfPCell background color ignoring alpha channel of an rgba #282

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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