Color Class
The ColorClass
is the core class of the bycolors package, providing a rich interface for color manipulation and transformation.
Constructor
ColorClass(palette: Union[str, Tuple[float, float, float], Dict], background_color: Optional[Tuple[float, float, float]] = None)
Parameters
-
palette
: The color value. Can be:- A hex string (e.g., "#FF0000")
- An RGB/RGBA tuple (e.g., (1, 0, 0) or (1, 0, 0, 1))
- A dictionary with 'main', 'dark', and 'light' variants
-
background_color
: Optional RGB tuple for alpha compositing. Defaults to white (1, 1, 1)
Properties
Color Variants
.dark
: Get a darker variant of the color (35% brightness if not specified in palette).light
: Get a lighter variant of the color (65% brightness if not specified in palette).main
: Get the main color instance.transparent
: Get a fully transparent version of the color
Color Formats
.rgb
: Get RGB values (0-1 range).rgba
: Get RGBA values (0-1 range).hex
: Get hexadecimal color string
Methods
Opacity Control
.opacity(opacity: float, background_color: Optional[Tuple[float, float, float]] = None)
.alpha(alpha: float, background_color: Optional[Tuple[float, float, float]] = None) # alias for opacity
Control the transparency of the color. Values range from 0 (transparent) to 1 (opaque).
Brightness Control
.brightness(brightness: float) # Relative brightness change
.absolute_brightness(brightness: float) # Absolute brightness value
Adjust the color brightness:
- brightness()
: Values < 0.5 darken, > 0.5 lighten
- absolute_brightness()
: Set exact brightness (0 = black, 1 = white)
Examples
# Create a color
blue = ColorClass("#0000FF")
# Get variants
dark_blue = blue.dark
light_blue = blue.light
# Modify opacity
semi_transparent = blue.opacity(0.5)
semi_transparent_on_black = blue.opacity(0.5, background_color=(0, 0, 0))
# Adjust brightness
darker = blue.brightness(0.3)
specific_brightness = blue.absolute_brightness(0.7)
# Get color values
rgb = blue.rgb # (0, 0, 1)
rgba = blue.rgba # (0, 0, 1, 1)
hex_color = blue.hex # "#0000FF"
bycolors.color_class.ColorClass
Bases: tuple
A class representing a color with various manipulation capabilities.
The ColorClass extends tuple to represent colors in RGB or RGBA format while providing methods for color manipulation like brightness adjustment, opacity changes, and color variant access (dark/light).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
palette
|
_PALETTE_TYPE | str | tuple[int, int, int] | tuple[int, int, int, int] | None
|
The color value, can be a hex string, RGB/RGBA tuple, or a palette dictionary with 'main', 'dark', and 'light' variants. |
None
|
background_color
|
_RGB_TYPE | None
|
The background color used for alpha compositing. Defaults to white (1, 1, 1). |
None
|
Examples:
>>> blue = ColorClass("#0000FF")
>>> semi_transparent = blue.opacity(0.5)
>>> darker_blue = blue.brightness(0.35)
>>> rgb_values = blue.rgb # (0, 0, 1)
Source code in python/bycolors/color_class.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
|
dark
property
dark
Returns a darker variant of the color.
If a dark variant is defined in the palette, returns that. Otherwise, returns a color with 35% brightness of the original.
Returns:
Name | Type | Description |
---|---|---|
ColorClass |
A new color instance with darker values. |
hex
property
hex
Returns the color in hexadecimal format.
Returns:
Name | Type | Description |
---|---|---|
str |
Color in hex format (e.g., "#FF0000" for red). |
light
property
light
Returns a lighter variant of the color.
If a light variant is defined in the palette, returns that. Otherwise, returns a color with 65% brightness of the original.
Returns:
Name | Type | Description |
---|---|---|
ColorClass |
A new color instance with lighter values. |
main
property
main
Returns the main color instance.
Returns:
Name | Type | Description |
---|---|---|
ColorClass |
The current color instance. |
rgb
property
rgb
Returns the color as RGB values, handling alpha compositing if needed.
If the color has alpha < 1, performs alpha compositing with the background color.
Returns:
Type | Description |
---|---|
tuple[float, float, float]: Color as RGB values between 0 and 1. |
rgba
property
rgba
Returns the color as RGBA values.
Returns:
Type | Description |
---|---|
tuple[float, float, float, float]: Color as RGBA values between 0 and 1. |
transparent
property
transparent
Returns a fully transparent version of the color.
Returns:
Name | Type | Description |
---|---|---|
ColorClass |
New color instance with zero opacity. |
absolute_brightness
absolute_brightness(brightness)
Changes the brightness of the color to an absolute value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
brightness
|
float
|
Target brightness between 0 and 1. 0 is black, 1 is white. |
required |
Returns:
Name | Type | Description |
---|---|---|
ColorClass |
New color instance with modified brightness. |
Source code in python/bycolors/color_class.py
320 321 322 323 324 325 326 327 328 329 330 331 332 |
|
alpha
alpha(alpha, background_color=None)
Alias for opacity method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha
|
float
|
Alpha value between 0 and 1. |
required |
background_color
|
_RGB_TYPE | None
|
Optional background color for compositing. |
None
|
Returns:
Name | Type | Description |
---|---|---|
ColorClass |
New color instance with modified alpha. |
Source code in python/bycolors/color_class.py
280 281 282 283 284 285 286 287 288 289 290 |
|
brightness
brightness(brightness)
Changes the brightness of the color relatively.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
brightness
|
float
|
Target brightness between 0 and 1. Values < 0.5 darken the color, values > 0.5 lighten it. |
required |
Returns:
Name | Type | Description |
---|---|---|
ColorClass |
New color instance with modified brightness. |
Source code in python/bycolors/color_class.py
306 307 308 309 310 311 312 313 314 315 316 317 318 |
|
opacity
opacity(opacity, background_color=None)
Creates a new color with modified opacity.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
opacity
|
float
|
Opacity value between 0 and 1. |
required |
background_color
|
_RGB_TYPE | None
|
Optional background color for compositing. |
None
|
Returns:
Name | Type | Description |
---|---|---|
ColorClass |
New color instance with modified opacity. |
Source code in python/bycolors/color_class.py
292 293 294 295 296 297 298 299 300 301 302 303 304 |
|