Record Class PdfColor

java.lang.Object
java.lang.Record
cloud.opencode.base.pdf.content.PdfColor
Record Components:
red - red component (0-1) | 红色分量
green - green component (0-1) | 绿色分量
blue - blue component (0-1) | 蓝色分量
alpha - alpha (0-1, 0=transparent, 1=opaque) | 透明度
model - color model | 颜色模型

public record PdfColor(float red, float green, float blue, float alpha, PdfColor.ColorModel model) extends Record
PDF Color - Color representation for PDF content PDF 颜色 - PDF 内容的颜色表示

Supports RGB, CMYK, and grayscale color models.

支持 RGB、CMYK 和灰度颜色模型。

Features | 主要功能:

  • RGB, RGBA, and grayscale color creation - RGB、RGBA 和灰度颜色创建
  • Hex string parsing - 十六进制字符串解析
  • Color manipulation (darker, lighter, alpha) - 颜色操作(加深、变浅、透明度)
  • Predefined color constants - 预定义颜色常量

Usage Examples | 使用示例:

PdfColor red = PdfColor.rgb(255, 0, 0);
PdfColor custom = PdfColor.hex("#3366CC");
PdfColor semiTransparent = PdfColor.rgba(0, 0, 0, 0.5f);
PdfColor darkRed = PdfColor.RED.darker(0.7f);

Security | 安全性:

  • Thread-safe: Yes — immutable record - 线程安全: 是 — 不可变记录
  • Null-safe: No — factory methods do not validate null inputs - 空值安全: 否 — 工厂方法不验证空输入
Since:
JDK 25, opencode-base-pdf V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    Color Model 颜色模型
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final PdfColor
    Black color | 黑色
    static final PdfColor
    Blue color | 蓝色
    static final PdfColor
    Cyan color | 青色
    static final PdfColor
    Dark gray color | 深灰色
    static final PdfColor
    Gray color | 灰色
    static final PdfColor
    Green color | 绿色
    static final PdfColor
    Light gray color | 浅灰色
    static final PdfColor
    Magenta color | 品红色
    static final PdfColor
    Orange color | 橙色
    static final PdfColor
    Red color | 红色
    static final PdfColor
    White color | 白色
    static final PdfColor
    Yellow color | 黄色
  • Constructor Summary

    Constructors
    Constructor
    Description
    PdfColor(float red, float green, float blue, float alpha, PdfColor.ColorModel model)
    Creates an instance of a PdfColor record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    float
    Returns the value of the alpha record component.
    float
    Returns the value of the blue record component.
    darker(float factor)
    Creates a darker version of this color 创建当前颜色的深色版本
    final boolean
    Indicates whether some other object is "equal to" this one.
    float
    Gets alpha (opacity) 获取透明度
    float
    Gets blue component (0-1) 获取蓝色分量 (0-1)
    float
    Gets green component (0-1) 获取绿色分量 (0-1)
    Gets color model 获取颜色模型
    float
    Gets red component (0-1) 获取红色分量 (0-1)
    static PdfColor
    gray(float gray)
    Creates grayscale color from 0-1 value 从 0-1 值创建灰度颜色
    static PdfColor
    gray(int gray)
    Creates grayscale color 创建灰度颜色
    float
    Returns the value of the green record component.
    final int
    Returns a hash code value for this object.
    static PdfColor
    hex(String hex)
    Creates color from hex string 从十六进制字符串创建颜色
    lighter(float factor)
    Creates a lighter version of this color 创建当前颜色的浅色版本
    Returns the value of the model record component.
    float
    red()
    Returns the value of the red record component.
    static PdfColor
    rgb(float r, float g, float b)
    Creates RGB color from 0-1 values 从 0-1 值创建 RGB 颜色
    static PdfColor
    rgb(int r, int g, int b)
    Creates RGB color from 0-255 values 从 0-255 值创建 RGB 颜色
    static PdfColor
    rgba(int r, int g, int b, float alpha)
    Creates RGBA color with alpha 创建带透明度的 RGBA 颜色
    Converts to hex string 转换为十六进制字符串
    Returns a string representation of this record class.
    withAlpha(float newAlpha)
    Creates a color with different alpha 创建具有不同透明度的颜色

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • BLACK

      public static final PdfColor BLACK
      Black color | 黑色
    • WHITE

      public static final PdfColor WHITE
      White color | 白色
    • RED

      public static final PdfColor RED
      Red color | 红色
    • GREEN

      public static final PdfColor GREEN
      Green color | 绿色
    • BLUE

      public static final PdfColor BLUE
      Blue color | 蓝色
    • GRAY

      public static final PdfColor GRAY
      Gray color | 灰色
    • LIGHT_GRAY

      public static final PdfColor LIGHT_GRAY
      Light gray color | 浅灰色
    • DARK_GRAY

      public static final PdfColor DARK_GRAY
      Dark gray color | 深灰色
    • YELLOW

      public static final PdfColor YELLOW
      Yellow color | 黄色
    • CYAN

      public static final PdfColor CYAN
      Cyan color | 青色
    • MAGENTA

      public static final PdfColor MAGENTA
      Magenta color | 品红色
    • ORANGE

      public static final PdfColor ORANGE
      Orange color | 橙色
  • Constructor Details

    • PdfColor

      public PdfColor(float red, float green, float blue, float alpha, PdfColor.ColorModel model)
      Creates an instance of a PdfColor record class.
      Parameters:
      red - the value for the red record component
      green - the value for the green record component
      blue - the value for the blue record component
      alpha - the value for the alpha record component
      model - the value for the model record component
  • Method Details

    • rgb

      public static PdfColor rgb(int r, int g, int b)
      Creates RGB color from 0-255 values 从 0-255 值创建 RGB 颜色
      Parameters:
      r - red component (0-255) | 红色分量
      g - green component (0-255) | 绿色分量
      b - blue component (0-255) | 蓝色分量
      Returns:
      PdfColor instance | PdfColor 实例
    • rgb

      public static PdfColor rgb(float r, float g, float b)
      Creates RGB color from 0-1 values 从 0-1 值创建 RGB 颜色
      Parameters:
      r - red component (0-1) | 红色分量
      g - green component (0-1) | 绿色分量
      b - blue component (0-1) | 蓝色分量
      Returns:
      PdfColor instance | PdfColor 实例
    • rgba

      public static PdfColor rgba(int r, int g, int b, float alpha)
      Creates RGBA color with alpha 创建带透明度的 RGBA 颜色
      Parameters:
      r - red component (0-255) | 红色分量
      g - green component (0-255) | 绿色分量
      b - blue component (0-255) | 蓝色分量
      alpha - alpha (0-1, 0=transparent, 1=opaque) | 透明度
      Returns:
      PdfColor instance | PdfColor 实例
    • gray

      public static PdfColor gray(int gray)
      Creates grayscale color 创建灰度颜色
      Parameters:
      gray - gray level (0-255) | 灰度级别
      Returns:
      PdfColor instance | PdfColor 实例
    • gray

      public static PdfColor gray(float gray)
      Creates grayscale color from 0-1 value 从 0-1 值创建灰度颜色
      Parameters:
      gray - gray level (0-1) | 灰度级别
      Returns:
      PdfColor instance | PdfColor 实例
    • hex

      public static PdfColor hex(String hex)
      Creates color from hex string 从十六进制字符串创建颜色
      Parameters:
      hex - hex color (e.g., "#FF0000" or "FF0000") | 十六进制颜色
      Returns:
      PdfColor instance | PdfColor 实例
    • getRed

      public float getRed()
      Gets red component (0-1) 获取红色分量 (0-1)
      Returns:
      red component | 红色分量
    • getGreen

      public float getGreen()
      Gets green component (0-1) 获取绿色分量 (0-1)
      Returns:
      green component | 绿色分量
    • getBlue

      public float getBlue()
      Gets blue component (0-1) 获取蓝色分量 (0-1)
      Returns:
      blue component | 蓝色分量
    • getAlpha

      public float getAlpha()
      Gets alpha (opacity) 获取透明度
      Returns:
      alpha (0-1) | 透明度
    • getModel

      public PdfColor.ColorModel getModel()
      Gets color model 获取颜色模型
      Returns:
      color model | 颜色模型
    • toHex

      public String toHex()
      Converts to hex string 转换为十六进制字符串
      Returns:
      hex string (e.g., "#FF0000") | 十六进制字符串
    • darker

      public PdfColor darker(float factor)
      Creates a darker version of this color 创建当前颜色的深色版本
      Parameters:
      factor - darkening factor (0-1) | 加深因子
      Returns:
      darker color | 深色颜色
    • lighter

      public PdfColor lighter(float factor)
      Creates a lighter version of this color 创建当前颜色的浅色版本
      Parameters:
      factor - lightening factor (0-1) | 变浅因子
      Returns:
      lighter color | 浅色颜色
    • withAlpha

      public PdfColor withAlpha(float newAlpha)
      Creates a color with different alpha 创建具有不同透明度的颜色
      Parameters:
      newAlpha - new alpha value | 新的透明度值
      Returns:
      color with new alpha | 具有新透明度的颜色
    • toString

      public String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with the compare method from their corresponding wrapper classes.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • red

      public float red()
      Returns the value of the red record component.
      Returns:
      the value of the red record component
    • green

      public float green()
      Returns the value of the green record component.
      Returns:
      the value of the green record component
    • blue

      public float blue()
      Returns the value of the blue record component.
      Returns:
      the value of the blue record component
    • alpha

      public float alpha()
      Returns the value of the alpha record component.
      Returns:
      the value of the alpha record component
    • model

      public PdfColor.ColorModel model()
      Returns the value of the model record component.
      Returns:
      the value of the model record component