|
6 | 6 |
|
7 | 7 | Methods: |
8 | 8 | - rgb_to_hex(r, g, b): Converts RGB to HEX. |
9 | | - - hex_to_rgb(hex_value): Converts HEX to RGB. |
10 | 9 | - rgb_to_hsl(r, g, b): Converts RGB to HSL. |
11 | | - - hsl_to_rgb(h, s, l): Converts HSL to RGB. |
12 | 10 | - rgb_to_hsv(r, g, b): Converts RGB to HSV. |
13 | | - - hsv_to_rgb(h, s, v): Converts HSV to RGB. |
14 | 11 | - rgb_to_cmyk(r, g, b): Converts RGB to CMYK. |
| 12 | + - rgb_to_xyz(r, g, b): Converts RGB to XYZ. |
| 13 | + - hex_to_rgb(hex_value): Converts HEX to RGB. |
| 14 | + - hex_to_hsl(hex_value): Converts HEX to HSL. |
| 15 | + - hex_to_hsv(hex_value): Converts HEX to HSV. |
| 16 | + - hex_to_cmyk(hex_value): Converts HEX to CMYK. |
| 17 | + - hex_to_xyz(hex_value): Converts HEX to XYZ. |
| 18 | + - hsl_to_rgb(h, s, l): Converts HSL to RGB. |
| 19 | + - hsl_to_hex(hsl_value): Converts HSL to HEX. |
| 20 | + - hsl_to_hsv(hsl_value): Converts HSL to HSV. |
| 21 | + - hsl_to_cmyk(hsl_value): Converts HSL to CMYK. |
| 22 | + - hsl_to_xyz(hsl_value): Converts HSL to XYZ. |
| 23 | + - hsv_to_rgb(h, s, v): Converts HSV to RGB. |
| 24 | + - hsv_to_hex(hsv_value): Converts HSV to HEX. |
| 25 | + - hsv_to_hsl(hsv_value): Converts HSV to HSL. |
| 26 | + - hsv_to_cmyk(hsv_value): Converts HSV to CMYK. |
| 27 | + - hsv_to_xyz(hsv_value): Converts HSV to XYZ. |
15 | 28 | - cmyk_to_rgb(c, m, y, k): Converts CMYK to RGB. |
| 29 | + - cmyk_to_hex(cmyk_value): Converts CMYK to HEX. |
| 30 | + - cmyk_to_hsl(cmyk_value): Converts CMYK to HSL. |
| 31 | + - cmyk_to_hsv(cmyk_value): Converts CMYK to HSV. |
| 32 | + - cmyk_to_xyz(cmyk_value): Converts CMYK to XYZ. |
| 33 | + - xyz_to_rgb(x, y, z): Converts XYZ to RGB. |
| 34 | + - xyz_to_hex(xyz_value): Converts XYZ to HEX. |
| 35 | + - xyz_to_hsl(xyz_value): Converts XYZ to HSL. |
| 36 | + - xyz_to_hsv(xyz_value): Converts XYZ to HSV. |
| 37 | + - xyz_to_cmyk(xyz_value): Converts XYZ to CMYK. |
| 38 | + - blend_colors(color1, color2, ratio=0.5): Blends two colors in the RGB format (tuple) using the specified ratio. |
16 | 39 | """ |
17 | 40 |
|
18 | 41 | def rgb_to_hex(r, g, b): |
@@ -495,6 +518,178 @@ def cmyk_to_hsl(cmyk_value: tuple) -> tuple: |
495 | 518 |
|
496 | 519 | return rgb_to_hsl(*cmyk_to_rgb(c, m, y, k)) |
497 | 520 |
|
| 521 | +def rgb_to_xyz(r: int, g: int, b: int) -> tuple: |
| 522 | + """ |
| 523 | + Converts RGB (Red, Green, Blue) to CIE 1931 XYZ color space. |
| 524 | +
|
| 525 | + Parameters: |
| 526 | + r (int): Red component, in the range [0, 255]. |
| 527 | + g (int): Green component, in the range [0, 255]. |
| 528 | + b (int): Blue component, in the range [0, 255]. |
| 529 | +
|
| 530 | + Returns: |
| 531 | + tuple: A tuple representing the XYZ values (x, y, z), where: |
| 532 | + x, y, z (float): Correspond to the CIE 1931 color space. |
| 533 | +
|
| 534 | + Raises: |
| 535 | + ValueError: If any RGB value is out of range. |
| 536 | + """ |
| 537 | + if not (0 <= r <= 255 and 0 <= g <= 255 and 0 <= b <= 255): |
| 538 | + raise ValueError("RGB values must be in the range [0, 255].") |
| 539 | + |
| 540 | + r_linear, g_linear, b_linear = [ |
| 541 | + (c / 255) ** 2.2 if c > 0.04045 else c / 255 / 12.92 for c in (r, g, b) |
| 542 | + ] |
| 543 | + |
| 544 | + x = 0.412453 * r_linear + 0.357580 * g_linear + 0.180423 * b_linear |
| 545 | + y = 0.212671 * r_linear + 0.715160 * g_linear + 0.072169 * b_linear |
| 546 | + z = 0.019334 * r_linear + 0.119193 * g_linear + 0.950227 * b_linear |
| 547 | + |
| 548 | + return x, y, z |
| 549 | + |
| 550 | + |
| 551 | +def xyz_to_rgb(x: float, y: float, z: float) -> tuple: |
| 552 | + """ |
| 553 | + Converts CIE 1931 XYZ to RGB (Red, Green, Blue) color space. |
| 554 | +
|
| 555 | + Parameters: |
| 556 | + x (float): X component of the XYZ color space. |
| 557 | + y (float): Y component of the XYZ color space. |
| 558 | + z (float): Z component of the XYZ color space. |
| 559 | +
|
| 560 | + Returns: |
| 561 | + tuple: A tuple representing the RGB values (r, g, b), where: |
| 562 | + r, g, b (int): Red, Green, and Blue components, each in the range [0, 255]. |
| 563 | +
|
| 564 | + Raises: |
| 565 | + ValueError: If any resulting RGB values are outside the valid range after conversion. |
| 566 | + """ |
| 567 | + r_linear = 3.240479 * x - 1.537150 * y - 0.498535 * z |
| 568 | + g_linear = -0.969256 * x + 1.875992 * y + 0.041556 * z |
| 569 | + b_linear = 0.055648 * x - 0.204043 * y + 1.057311 * z |
| 570 | + |
| 571 | + r = r_linear ** (1 / 2.2) |
| 572 | + g = g_linear ** (1 / 2.2) |
| 573 | + b = b_linear ** (1 / 2.2) |
| 574 | + |
| 575 | + r = max(0, min(r, 1)) * 255 |
| 576 | + g = max(0, min(g, 1)) * 255 |
| 577 | + b = max(0, min(b, 1)) * 255 |
| 578 | + |
| 579 | + return round(r), round(g), round(b) |
| 580 | + |
| 581 | + |
| 582 | +def hex_to_xyz(hex_value: str) -> tuple: |
| 583 | + """ |
| 584 | + Converts a hexadecimal color code to CIE 1931 XYZ color space. |
| 585 | +
|
| 586 | + Parameters: |
| 587 | + hex_value (str): A hexadecimal color code (e.g., '#RRGGBB'). |
| 588 | +
|
| 589 | + Returns: |
| 590 | + tuple: A tuple representing the XYZ values (x, y, z). |
| 591 | + """ |
| 592 | + r, g, b = hex_to_rgb(hex_value) |
| 593 | + return rgb_to_xyz(r, g, b) |
| 594 | + |
| 595 | + |
| 596 | +def xyz_to_hex(xyz_value: tuple) -> str: |
| 597 | + """ |
| 598 | + Converts CIE 1931 XYZ to a hexadecimal color code. |
| 599 | +
|
| 600 | + Parameters: |
| 601 | + xyz_value (tuple): A tuple of XYZ values (x, y, z). |
| 602 | +
|
| 603 | + Returns: |
| 604 | + str: A hexadecimal color code (e.g., '#RRGGBB'). |
| 605 | + """ |
| 606 | + r, g, b = xyz_to_rgb(*xyz_value) |
| 607 | + return rgb_to_hex(r, g, b) |
| 608 | + |
| 609 | + |
| 610 | +def hsl_to_xyz(hsl_value: tuple) -> tuple: |
| 611 | + """ |
| 612 | + Converts HSL (Hue, Saturation, Lightness) to CIE 1931 XYZ color space. |
| 613 | +
|
| 614 | + Parameters: |
| 615 | + hsl_value (tuple): A tuple representing the HSL values (h, s, l). |
| 616 | +
|
| 617 | + Returns: |
| 618 | + tuple: A tuple representing the XYZ values (x, y, z). |
| 619 | + """ |
| 620 | + r, g, b = hsl_to_rgb(*hsl_value) |
| 621 | + return rgb_to_xyz(r, g, b) |
| 622 | + |
| 623 | + |
| 624 | +def xyz_to_hsl(xyz_value: tuple) -> tuple: |
| 625 | + """ |
| 626 | + Converts CIE 1931 XYZ to HSL (Hue, Saturation, Lightness). |
| 627 | +
|
| 628 | + Parameters: |
| 629 | + xyz_value (tuple): A tuple of XYZ values (x, y, z). |
| 630 | +
|
| 631 | + Returns: |
| 632 | + tuple: A tuple representing the HSL values (h, s, l). |
| 633 | + """ |
| 634 | + r, g, b = xyz_to_rgb(*xyz_value) |
| 635 | + return rgb_to_hsl(r, g, b) |
| 636 | + |
| 637 | + |
| 638 | +def hsv_to_xyz(hsv_value: tuple) -> tuple: |
| 639 | + """ |
| 640 | + Converts HSV (Hue, Saturation, Value) to CIE 1931 XYZ color space. |
| 641 | +
|
| 642 | + Parameters: |
| 643 | + hsv_value (tuple): A tuple representing the HSV values (h, s, v). |
| 644 | +
|
| 645 | + Returns: |
| 646 | + tuple: A tuple representing the XYZ values (x, y, z). |
| 647 | + """ |
| 648 | + r, g, b = hsv_to_rgb(*hsv_value) |
| 649 | + return rgb_to_xyz(r, g, b) |
| 650 | + |
| 651 | + |
| 652 | +def xyz_to_hsv(xyz_value: tuple) -> tuple: |
| 653 | + """ |
| 654 | + Converts CIE 1931 XYZ to HSV (Hue, Saturation, Value). |
| 655 | +
|
| 656 | + Parameters: |
| 657 | + xyz_value (tuple): A tuple of XYZ values (x, y, z). |
| 658 | +
|
| 659 | + Returns: |
| 660 | + tuple: A tuple representing the HSV values (h, s, v). |
| 661 | + """ |
| 662 | + r, g, b = xyz_to_rgb(*xyz_value) |
| 663 | + return rgb_to_hsv(r, g, b) |
| 664 | + |
| 665 | + |
| 666 | +def xyz_to_cmyk(xyz_value: tuple) -> tuple: |
| 667 | + """ |
| 668 | + Converts CIE 1931 XYZ to CMYK (Cyan, Magenta, Yellow, Key/Black). |
| 669 | +
|
| 670 | + Parameters: |
| 671 | + xyz_value (tuple): A tuple of XYZ values (x, y, z). |
| 672 | +
|
| 673 | + Returns: |
| 674 | + tuple: A tuple representing the CMYK values (c, m, y, k). |
| 675 | + """ |
| 676 | + r, g, b = xyz_to_rgb(*xyz_value) |
| 677 | + return rgb_to_cmyk(r, g, b) |
| 678 | + |
| 679 | + |
| 680 | +def cmyk_to_xyz(cmyk_value: tuple) -> tuple: |
| 681 | + """ |
| 682 | + Converts CMYK (Cyan, Magenta, Yellow, Key/Black) to CIE 1931 XYZ color space. |
| 683 | +
|
| 684 | + Parameters: |
| 685 | + cmyk_value (tuple): A tuple of CMYK values (c, m, y, k). |
| 686 | +
|
| 687 | + Returns: |
| 688 | + tuple: A tuple representing the XYZ values (x, y, z). |
| 689 | + """ |
| 690 | + r, g, b = cmyk_to_rgb(*cmyk_value) |
| 691 | + return rgb_to_xyz(r, g, b) |
| 692 | + |
498 | 693 | def blend_colors(color1, color2, ratio=0.5): |
499 | 694 | """ |
500 | 695 | Blends two colors in the RGB format using the specified ratio. |
|
0 commit comments