Giter VIP home page Giter VIP logo

Comments (5)

siegelzc avatar siegelzc commented on August 18, 2024 1

openhtmltopdf does not support opacity

it looks like you may be using the alpha channel here as an analogue for HSL-style "lightness". you can achieve this effect by rendering the html as you would expect in a browser, and then using an eyedropper tool to get the RGB value of the rendered color

from openhtmltopdf.

siegelzc avatar siegelzc commented on August 18, 2024 1

BTW, the community is in the process of transitioning to a new repo at https://github.com/openhtmltopdf/openhtmltopdf because danfickle is no longer maintaining this repository.

from openhtmltopdf.

LuoHpeng avatar LuoHpeng commented on August 18, 2024 1

The following method converts rgba in html to rgb

public static final Color BACK_GROUND = new Color(255, 255, 255);

public static final String RGBA_REGEX = "rgba\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,\s*([\d.]+)\s*\)";

public static final Pattern PATTERN = Pattern.compile(RGBA_REGEX);

public static String rgbaToRgb(String htmlString) {

Matcher matcher = PATTERN.matcher(htmlString);

// Replace the matching RGBA color to RGB format
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
  int red = Integer.parseInt(matcher.group(1));
  int green = Integer.parseInt(matcher.group(2));
  int blue = Integer.parseInt(matcher.group(3));
  // alpha
  float alpha = Float.parseFloat(matcher.group(4));
  red = (int) (red * alpha + BACK_GROUND.getRed() * (1 - alpha));
  green = (int) (green * alpha + BACK_GROUND.getGreen() * (1 - alpha));
  blue = (int) (blue * alpha + BACK_GROUND.getBlue() * (1 - alpha));
  // Convert RGBA to RGB
  String rgbColor = "rgb(" + red + ", " + green + ", " + blue + ")";
  matcher.appendReplacement(sb, rgbColor);
}
matcher.appendTail(sb);
return sb.toString();

}

from openhtmltopdf.

LuoHpeng avatar LuoHpeng commented on August 18, 2024

WechatIMG33126

from openhtmltopdf.

siegelzc avatar siegelzc commented on August 18, 2024

Feel free to open an issue at the other repo with this code snippet. It would be cool if we could add transparency support.

from openhtmltopdf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.