diff options
-rw-r--r-- | 10deg | 391 | ||||
-rw-r--r-- | 2deg | 391 | ||||
-rwxr-xr-x | src/__main__.py | 219 | ||||
-rw-r--r-- | src/colour.py | 56 |
4 files changed, 1028 insertions, 29 deletions
@@ -0,0 +1,391 @@ +0.6472 0.3506 +0.6348 0.3612 +0.6225 0.3710 +0.6104 0.3797 +0.5983 0.3874 +0.5864 0.3940 +0.5747 0.3996 +0.5632 0.4041 +0.5519 0.4077 +0.5408 0.4104 +0.5300 0.4123 +0.5195 0.4134 +0.5093 0.4139 +0.4995 0.4138 +0.4900 0.4132 +0.4808 0.4122 +0.4720 0.4108 +0.4636 0.4091 +0.4555 0.4071 +0.4478 0.4050 +0.4404 0.4026 +0.4333 0.4002 +0.4266 0.3976 +0.4202 0.3950 +0.4140 0.3923 +0.4082 0.3895 +0.4026 0.3868 +0.3973 0.3841 +0.3922 0.3813 +0.3873 0.3786 +0.3827 0.3759 +0.3783 0.3733 +0.3741 0.3707 +0.3701 0.3681 +0.3662 0.3656 +0.3625 0.3631 +0.3590 0.3607 +0.3557 0.3583 +0.3524 0.3560 +0.3494 0.3538 +0.3464 0.3516 +0.3436 0.3494 +0.3409 0.3473 +0.3383 0.3453 +0.3358 0.3433 +0.3334 0.3413 +0.3311 0.3394 +0.3289 0.3376 +0.3268 0.3358 +0.3247 0.3341 +0.3228 0.3324 +0.3209 0.3307 +0.3190 0.3291 +0.3173 0.3275 +0.3156 0.3260 +0.3140 0.3245 +0.3124 0.3231 +0.3109 0.3217 +0.3094 0.3203 +0.3080 0.3190 +0.3066 0.3177 +0.3053 0.3164 +0.3040 0.3152 +0.3028 0.3140 +0.3016 0.3128 +0.3004 0.3117 +0.2993 0.3106 +0.2982 0.3095 +0.2971 0.3084 +0.2961 0.3074 +0.2951 0.3064 +0.2942 0.3054 +0.2932 0.3044 +0.2923 0.3035 +0.2914 0.3026 +0.2906 0.3017 +0.2897 0.3008 +0.2889 0.3000 +0.2881 0.2992 +0.2874 0.2983 +0.2866 0.2976 +0.2859 0.2968 +0.2852 0.2960 +0.2845 0.2953 +0.2839 0.2946 +0.2832 0.2939 +0.2826 0.2932 +0.2820 0.2925 +0.2813 0.2918 +0.2808 0.2912 +0.2802 0.2905 +0.2796 0.2899 +0.2791 0.2893 +0.2785 0.2887 +0.2780 0.2881 +0.2775 0.2876 +0.2770 0.2870 +0.2765 0.2865 +0.2761 0.2859 +0.2756 0.2854 +0.2751 0.2849 +0.2747 0.2844 +0.2743 0.2839 +0.2738 0.2834 +0.2734 0.2829 +0.2730 0.2825 +0.2726 0.2820 +0.2722 0.2816 +0.2719 0.2811 +0.2715 0.2807 +0.2711 0.2803 +0.2708 0.2799 +0.2704 0.2794 +0.2701 0.2790 +0.2697 0.2786 +0.2694 0.2783 +0.2691 0.2779 +0.2688 0.2775 +0.2684 0.2771 +0.2681 0.2768 +0.2678 0.2764 +0.2675 0.2761 +0.2673 0.2757 +0.2670 0.2754 +0.2667 0.2751 +0.2664 0.2747 +0.2662 0.2744 +0.2659 0.2741 +0.2656 0.2738 +0.2654 0.2735 +0.2651 0.2732 +0.2649 0.2729 +0.2646 0.2726 +0.2644 0.2723 +0.2642 0.2720 +0.2639 0.2718 +0.2637 0.2715 +0.2635 0.2712 +0.2633 0.2709 +0.2631 0.2707 +0.2629 0.2704 +0.2626 0.2702 +0.2624 0.2699 +0.2622 0.2697 +0.2620 0.2694 +0.2618 0.2692 +0.2617 0.2690 +0.2615 0.2687 +0.2613 0.2685 +0.2611 0.2683 +0.2609 0.2681 +0.2607 0.2678 +0.2606 0.2676 +0.2604 0.2674 +0.2602 0.2672 +0.2601 0.2670 +0.2599 0.2668 +0.2597 0.2666 +0.2596 0.2664 +0.2594 0.2662 +0.2593 0.2660 +0.2591 0.2658 +0.2590 0.2656 +0.2588 0.2654 +0.2587 0.2653 +0.2585 0.2651 +0.2584 0.2649 +0.2582 0.2647 +0.2581 0.2645 +0.2580 0.2644 +0.2578 0.2642 +0.2577 0.2640 +0.2575 0.2639 +0.2574 0.2637 +0.2573 0.2635 +0.2572 0.2634 +0.2570 0.2632 +0.2569 0.2631 +0.2568 0.2629 +0.2567 0.2628 +0.2566 0.2626 +0.2564 0.2625 +0.2563 0.2623 +0.2562 0.2622 +0.2561 0.2620 +0.2560 0.2619 +0.2559 0.2618 +0.2558 0.2616 +0.2557 0.2615 +0.2555 0.2613 +0.2554 0.2612 +0.2553 0.2611 +0.2552 0.2609 +0.2551 0.2608 +0.2550 0.2607 +0.2549 0.2606 +0.2548 0.2604 +0.2547 0.2603 +0.2546 0.2602 +0.2546 0.2601 +0.2545 0.2600 +0.2544 0.2598 +0.2543 0.2597 +0.2542 0.2596 +0.2541 0.2595 +0.2540 0.2594 +0.2539 0.2593 +0.2538 0.2591 +0.2537 0.2590 +0.2537 0.2589 +0.2536 0.2588 +0.2535 0.2587 +0.2534 0.2586 +0.2533 0.2585 +0.2533 0.2584 +0.2532 0.2583 +0.2531 0.2582 +0.2530 0.2581 +0.2529 0.2580 +0.2529 0.2579 +0.2528 0.2578 +0.2527 0.2577 +0.2526 0.2576 +0.2526 0.2575 +0.2525 0.2574 +0.2524 0.2573 +0.2524 0.2572 +0.2523 0.2572 +0.2522 0.2571 +0.2521 0.2570 +0.2521 0.2569 +0.2520 0.2568 +0.2519 0.2567 +0.2519 0.2566 +0.2518 0.2565 +0.2517 0.2565 +0.2517 0.2564 +0.2516 0.2563 +0.2516 0.2562 +0.2515 0.2561 +0.2514 0.2560 +0.2514 0.2560 +0.2513 0.2559 +0.2512 0.2558 +0.2512 0.2557 +0.2511 0.2557 +0.2511 0.2556 +0.2510 0.2555 +0.2510 0.2554 +0.2509 0.2554 +0.2508 0.2553 +0.2508 0.2552 +0.2507 0.2551 +0.2507 0.2551 +0.2506 0.2550 +0.2506 0.2549 +0.2505 0.2548 +0.2505 0.2548 +0.2504 0.2547 +0.2503 0.2546 +0.2503 0.2546 +0.2502 0.2545 +0.2502 0.2544 +0.2501 0.2544 +0.2501 0.2543 +0.2500 0.2542 +0.2500 0.2542 +0.2499 0.2541 +0.2499 0.2541 +0.2499 0.2540 +0.2498 0.2539 +0.2498 0.2539 +0.2497 0.2538 +0.2497 0.2537 +0.2496 0.2537 +0.2496 0.2536 +0.2495 0.2536 +0.2495 0.2535 +0.2494 0.2534 +0.2494 0.2534 +0.2493 0.2533 +0.2493 0.2533 +0.2493 0.2532 +0.2492 0.2532 +0.2492 0.2531 +0.2491 0.2530 +0.2491 0.2530 +0.2490 0.2529 +0.2490 0.2529 +0.2490 0.2528 +0.2489 0.2528 +0.2489 0.2527 +0.2488 0.2527 +0.2488 0.2526 +0.2488 0.2526 +0.2487 0.2525 +0.2487 0.2525 +0.2487 0.2524 +0.2486 0.2524 +0.2486 0.2523 +0.2485 0.2523 +0.2485 0.2522 +0.2485 0.2522 +0.2484 0.2521 +0.2484 0.2521 +0.2484 0.2520 +0.2483 0.2520 +0.2483 0.2519 +0.2482 0.2519 +0.2482 0.2518 +0.2482 0.2518 +0.2481 0.2517 +0.2481 0.2517 +0.2481 0.2516 +0.2480 0.2516 +0.2480 0.2516 +0.2480 0.2515 +0.2479 0.2515 +0.2479 0.2514 +0.2479 0.2514 +0.2478 0.2513 +0.2478 0.2513 +0.2478 0.2513 +0.2477 0.2512 +0.2477 0.2512 +0.2477 0.2511 +0.2476 0.2511 +0.2476 0.2510 +0.2476 0.2510 +0.2476 0.2510 +0.2475 0.2509 +0.2475 0.2509 +0.2475 0.2508 +0.2474 0.2508 +0.2474 0.2508 +0.2474 0.2507 +0.2473 0.2507 +0.2473 0.2506 +0.2473 0.2506 +0.2473 0.2506 +0.2472 0.2505 +0.2472 0.2505 +0.2472 0.2505 +0.2471 0.2504 +0.2471 0.2504 +0.2471 0.2503 +0.2471 0.2503 +0.2470 0.2503 +0.2470 0.2502 +0.2470 0.2502 +0.2470 0.2502 +0.2469 0.2501 +0.2469 0.2501 +0.2469 0.2501 +0.2468 0.2500 +0.2468 0.2500 +0.2468 0.2500 +0.2468 0.2499 +0.2467 0.2499 +0.2467 0.2498 +0.2467 0.2498 +0.2467 0.2498 +0.2466 0.2497 +0.2466 0.2497 +0.2466 0.2497 +0.2466 0.2497 +0.2465 0.2496 +0.2465 0.2496 +0.2465 0.2496 +0.2465 0.2495 +0.2465 0.2495 +0.2464 0.2495 +0.2464 0.2494 +0.2464 0.2494 +0.2464 0.2494 +0.2463 0.2493 +0.2463 0.2493 +0.2463 0.2493 +0.2463 0.2492 +0.2462 0.2492 +0.2462 0.2492 +0.2462 0.2492 +0.2462 0.2491 +0.2462 0.2491 +0.2461 0.2491 +0.2461 0.2490 +0.2461 0.2490 +0.2461 0.2490 +0.2460 0.2490 +0.2460 0.2489 +0.2460 0.2489 @@ -0,0 +1,391 @@ +0.6499 0.3474 +0.6361 0.3594 +0.6226 0.3703 +0.6095 0.3801 +0.5966 0.3887 +0.5841 0.3962 +0.5720 0.4025 +0.5601 0.4076 +0.5486 0.4118 +0.5375 0.4150 +0.5267 0.4173 +0.5162 0.4188 +0.5062 0.4196 +0.4965 0.4198 +0.4872 0.4194 +0.4782 0.4186 +0.4696 0.4173 +0.4614 0.4158 +0.4535 0.4139 +0.4460 0.4118 +0.4388 0.4095 +0.4320 0.4070 +0.4254 0.4044 +0.4192 0.4018 +0.4132 0.3990 +0.4075 0.3962 +0.4021 0.3934 +0.3969 0.3905 +0.3919 0.3877 +0.3872 0.3849 +0.3827 0.3820 +0.3784 0.3793 +0.3743 0.3765 +0.3704 0.3738 +0.3666 0.3711 +0.3631 0.3685 +0.3596 0.3659 +0.3563 0.3634 +0.3532 0.3609 +0.3502 0.3585 +0.3473 0.3561 +0.3446 0.3538 +0.3419 0.3516 +0.3394 0.3494 +0.3369 0.3472 +0.3346 0.3451 +0.3323 0.3431 +0.3302 0.3411 +0.3281 0.3392 +0.3261 0.3373 +0.3242 0.3355 +0.3223 0.3337 +0.3205 0.3319 +0.3188 0.3302 +0.3171 0.3286 +0.3155 0.3270 +0.3140 0.3254 +0.3125 0.3238 +0.3110 0.3224 +0.3097 0.3209 +0.3083 0.3195 +0.3070 0.3181 +0.3058 0.3168 +0.3045 0.3154 +0.3034 0.3142 +0.3022 0.3129 +0.3011 0.3117 +0.3000 0.3105 +0.2990 0.3094 +0.2980 0.3082 +0.2970 0.3071 +0.2961 0.3061 +0.2952 0.3050 +0.2943 0.3040 +0.2934 0.3030 +0.2926 0.3020 +0.2917 0.3011 +0.2910 0.3001 +0.2902 0.2992 +0.2894 0.2983 +0.2887 0.2975 +0.2880 0.2966 +0.2873 0.2958 +0.2866 0.2950 +0.2860 0.2942 +0.2853 0.2934 +0.2847 0.2927 +0.2841 0.2919 +0.2835 0.2912 +0.2829 0.2905 +0.2824 0.2898 +0.2818 0.2891 +0.2813 0.2884 +0.2807 0.2878 +0.2802 0.2871 +0.2797 0.2865 +0.2792 0.2859 +0.2788 0.2853 +0.2783 0.2847 +0.2778 0.2841 +0.2774 0.2836 +0.2770 0.2830 +0.2765 0.2825 +0.2761 0.2819 +0.2757 0.2814 +0.2753 0.2809 +0.2749 0.2804 +0.2745 0.2799 +0.2742 0.2794 +0.2738 0.2789 +0.2734 0.2785 +0.2731 0.2780 +0.2727 0.2776 +0.2724 0.2771 +0.2721 0.2767 +0.2717 0.2763 +0.2714 0.2758 +0.2711 0.2754 +0.2708 0.2750 +0.2705 0.2746 +0.2702 0.2742 +0.2699 0.2738 +0.2696 0.2735 +0.2694 0.2731 +0.2691 0.2727 +0.2688 0.2724 +0.2686 0.2720 +0.2683 0.2717 +0.2680 0.2713 +0.2678 0.2710 +0.2675 0.2707 +0.2673 0.2703 +0.2671 0.2700 +0.2668 0.2697 +0.2666 0.2694 +0.2664 0.2691 +0.2662 0.2688 +0.2659 0.2685 +0.2657 0.2682 +0.2655 0.2679 +0.2653 0.2676 +0.2651 0.2673 +0.2649 0.2671 +0.2647 0.2668 +0.2645 0.2665 +0.2643 0.2663 +0.2641 0.2660 +0.2639 0.2657 +0.2638 0.2655 +0.2636 0.2652 +0.2634 0.2650 +0.2632 0.2648 +0.2631 0.2645 +0.2629 0.2643 +0.2627 0.2641 +0.2626 0.2638 +0.2624 0.2636 +0.2622 0.2634 +0.2621 0.2632 +0.2619 0.2629 +0.2618 0.2627 +0.2616 0.2625 +0.2615 0.2623 +0.2613 0.2621 +0.2612 0.2619 +0.2610 0.2617 +0.2609 0.2615 +0.2608 0.2613 +0.2606 0.2611 +0.2605 0.2609 +0.2604 0.2607 +0.2602 0.2606 +0.2601 0.2604 +0.2600 0.2602 +0.2598 0.2600 +0.2597 0.2598 +0.2596 0.2597 +0.2595 0.2595 +0.2593 0.2593 +0.2592 0.2592 +0.2591 0.2590 +0.2590 0.2588 +0.2589 0.2587 +0.2588 0.2585 +0.2587 0.2584 +0.2586 0.2582 +0.2584 0.2580 +0.2583 0.2579 +0.2582 0.2577 +0.2581 0.2576 +0.2580 0.2574 +0.2579 0.2573 +0.2578 0.2572 +0.2577 0.2570 +0.2576 0.2569 +0.2575 0.2567 +0.2574 0.2566 +0.2573 0.2565 +0.2572 0.2563 +0.2571 0.2562 +0.2571 0.2561 +0.2570 0.2559 +0.2569 0.2558 +0.2568 0.2557 +0.2567 0.2555 +0.2566 0.2554 +0.2565 0.2553 +0.2564 0.2552 +0.2564 0.2550 +0.2563 0.2549 +0.2562 0.2548 +0.2561 0.2547 +0.2560 0.2546 +0.2559 0.2545 +0.2559 0.2543 +0.2558 0.2542 +0.2557 0.2541 +0.2556 0.2540 +0.2556 0.2539 +0.2555 0.2538 +0.2554 0.2537 +0.2553 0.2536 +0.2553 0.2535 +0.2552 0.2534 +0.2551 0.2533 +0.2551 0.2532 +0.2550 0.2531 +0.2549 0.2530 +0.2548 0.2529 +0.2548 0.2528 +0.2547 0.2527 +0.2546 0.2526 +0.2546 0.2525 +0.2545 0.2524 +0.2544 0.2523 +0.2544 0.2522 +0.2543 0.2521 +0.2543 0.2520 +0.2542 0.2519 +0.2541 0.2518 +0.2541 0.2517 +0.2540 0.2516 +0.2540 0.2516 +0.2539 0.2515 +0.2538 0.2514 +0.2538 0.2513 +0.2537 0.2512 +0.2537 0.2511 +0.2536 0.2511 +0.2535 0.2510 +0.2535 0.2509 +0.2534 0.2508 +0.2534 0.2507 +0.2533 0.2507 +0.2533 0.2506 +0.2532 0.2505 +0.2532 0.2504 +0.2531 0.2503 +0.2531 0.2503 +0.2530 0.2502 +0.2530 0.2501 +0.2529 0.2500 +0.2529 0.2500 +0.2528 0.2499 +0.2528 0.2498 +0.2527 0.2497 +0.2527 0.2497 +0.2526 0.2496 +0.2526 0.2495 +0.2525 0.2495 +0.2525 0.2494 +0.2524 0.2493 +0.2524 0.2493 +0.2523 0.2492 +0.2523 0.2491 +0.2523 0.2491 +0.2522 0.2490 +0.2522 0.2489 +0.2521 0.2489 +0.2521 0.2488 +0.2520 0.2487 +0.2520 0.2487 +0.2519 0.2486 +0.2519 0.2485 +0.2519 0.2485 +0.2518 0.2484 +0.2518 0.2484 +0.2517 0.2483 +0.2517 0.2482 +0.2517 0.2482 +0.2516 0.2481 +0.2516 0.2481 +0.2515 0.2480 +0.2515 0.2480 +0.2515 0.2479 +0.2514 0.2478 +0.2514 0.2478 +0.2513 0.2477 +0.2513 0.2477 +0.2513 0.2476 +0.2512 0.2476 +0.2512 0.2475 +0.2512 0.2474 +0.2511 0.2474 +0.2511 0.2473 +0.2511 0.2473 +0.2510 0.2472 +0.2510 0.2472 +0.2509 0.2471 +0.2509 0.2471 +0.2509 0.2470 +0.2508 0.2470 +0.2508 0.2469 +0.2508 0.2469 +0.2507 0.2468 +0.2507 0.2468 +0.2507 0.2467 +0.2506 0.2467 +0.2506 0.2466 +0.2506 0.2466 +0.2505 0.2465 +0.2505 0.2465 +0.2505 0.2464 +0.2505 0.2464 +0.2504 0.2463 +0.2504 0.2463 +0.2504 0.2463 +0.2503 0.2462 +0.2503 0.2462 +0.2503 0.2461 +0.2502 0.2461 +0.2502 0.2460 +0.2502 0.2460 +0.2502 0.2459 +0.2501 0.2459 +0.2501 0.2459 +0.2501 0.2458 +0.2500 0.2458 +0.2500 0.2457 +0.2500 0.2457 +0.2500 0.2456 +0.2499 0.2456 +0.2499 0.2456 +0.2499 0.2455 +0.2498 0.2455 +0.2498 0.2454 +0.2498 0.2454 +0.2498 0.2454 +0.2497 0.2453 +0.2497 0.2453 +0.2497 0.2452 +0.2497 0.2452 +0.2496 0.2452 +0.2496 0.2451 +0.2496 0.2451 +0.2496 0.2450 +0.2495 0.2450 +0.2495 0.2450 +0.2495 0.2449 +0.2495 0.2449 +0.2494 0.2449 +0.2494 0.2448 +0.2494 0.2448 +0.2494 0.2447 +0.2493 0.2447 +0.2493 0.2447 +0.2493 0.2446 +0.2493 0.2446 +0.2492 0.2446 +0.2492 0.2445 +0.2492 0.2445 +0.2492 0.2445 +0.2491 0.2444 +0.2491 0.2444 +0.2491 0.2444 +0.2491 0.2443 +0.2491 0.2443 +0.2490 0.2443 +0.2490 0.2442 +0.2490 0.2442 +0.2490 0.2442 +0.2489 0.2441 +0.2489 0.2441 +0.2489 0.2441 +0.2489 0.2440 +0.2489 0.2440 +0.2488 0.2440 +0.2488 0.2439 +0.2488 0.2439 +0.2488 0.2439 +0.2487 0.2438 diff --git a/src/__main__.py b/src/__main__.py index 722f879..92b8cdc 100755 --- a/src/__main__.py +++ b/src/__main__.py @@ -16,10 +16,20 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. import math +from colour import * -r_curve = [i / 255 for i in range(256)] -g_curve = [i / 255 for i in range(256)] -b_curve = [i / 255 for i in range(256)] + +DATADIR = '.' + +i_size = 2 ** 8 +o_size = 2 ** 16 +r_curve = [i / (i_size - 1) for i in range(i_size)] +g_curve = [i / (i_size - 1) for i in range(i_size)] +b_curve = [i / (i_size - 1) for i in range(i_size)] +clip_result = True + +cmf_2deg_cache = None +cmf_10deg_cache = None def curves(r, g, b): @@ -34,26 +44,132 @@ def curves(r, g, b): return ((r_curve, r), (g_curve, g), (b_curve, b)) -def sigmoid(r, g, b): +def series_d(temperature): ''' - Apply S-curve correction on the colour curves + Calculate the colour for a blackbody temperature - @param r:float The sigmoid parameter for the red curve - @param g:float The sigmoid parameter for the green curve - @param b:float The sigmoid parameter for the blue curve + @param temperature:float The blackbody temperature in kelvins, must be inside [4000, 7000] + @return :(float, float, float) The red, green and blue components of the white point ''' - for (curve, level) in curves(r, g, b): - if not level == 1.0: - for i in range(256): - try: - curve[i] = 0.5 - math.log(1 / curve[i] - 1) / level - except: - curve[i] = 0; + x = 0 + ks = ((0.244063, 0), (0.09911, 1), (2.9678, 2), (-4.6070, 3)) + if temperature > 7000: + ks = ((0.237040, 0), (0.24748, 1), (1.9018, 2), (-2.0064, 3)) + for (k, d) in ks: + x += k * 10 ** (d * 3) / temperature ** d + y = 2.870 * x - 3.000 * x ** 2 - 0.275 + return to_srgb(x, y, 1.0) + +def simple_whitepoint(temperature): + ''' + Calculate the colour for a blackbody temperature using a simple, but inaccurate, algorithm + + @param temperature:float The blackbody temperature in kelvins, not guaranteed for values outside [1000, 40000] + @return :(float, float, float) The red, green and blue components of the white point + ''' + r, g, b = 1, 1, 1 + temp = temperature / 100 + if temp > 66: + temp -= 60 + r = 1.292936186 * temp ** 0.1332047592 + g = 1.129890861 * temp ** -0.0755148492 + else: + g = 0.390081579 * math.log(temp) - 0.631841444 + if temp <= 19: + b = 0 + elif temp < 66: + b = 0.543206789 * math.log(temp - 10) - 1.196254089 + return (r, g, b) + +def cmf_2deg(temperature): + ''' + Calculate the colour for a blackbody temperature using raw CIE 1931 2 degree CMF data with interpolation + + @param temperature:float The blackbody temperature in kelvins, clipped to [1000, 40000] + @return :(float, float, float) The red, green and blue components of the white point + ''' + if cmf_2deg_cache is None: + with open(DATADIR + '/2deg', 'rb') as file: + cmf_2deg_cache = file.read() + cmf_2deg_cache.decode('utf-8', 'error').split('\n') + cmf_2deg_cache = [[float(x) for x in x_y.split(' ')] for x_y in cmf_2deg_cache] + temperature = min(max(0, temperature), 1000) + x, y = 0, 0 + if (temp % 100) == 0: + (x, y) = temperature[(temp - 1000) // 100] + else: + temp -= 1000 + (x1, y1) = temperature[temp // 100] + (x2, y2) = temperature[temp // 100 + 1] + temp = (temp % 100) / 100 + x = x1 * temp + x2 * (1 - temp) + y = y1 * temp + y2 * (1 - temp) + return to_srgb(x, y, 1.0) + +def cmf_10deg(temperature): + ''' + Calculate the colour for a blackbody temperature using raw CIE 1964 10 degree CMF data with interpolation + + @param temperature:float The blackbody temperature in kelvins, clipped to [1000, 40000] + @return :(float, float, float) The red, green and blue components of the white point + ''' + if cmf_2deg_cache is None: + with open(DATADIR + '/10deg', 'rb') as file: + cmf_2deg_cache = file.read() + cmf_2deg_cache.decode('utf-8', 'error').split('\n') + cmf_2deg_cache = [[float(x) for x in x_y.split(' ')] for x_y in cmf_2deg_cache] + temperature = min(max(0, temperature), 1000) + x, y = 0, 0 + if (temp % 100) == 0: + (x, y) = temperature[(temp - 1000) // 100] + else: + temp -= 1000 + (x1, y1) = temperature[temp // 100] + (x2, y2) = temperature[temp // 100 + 1] + temp = (temp % 100) / 100 + x = x1 * temp + x2 * (1 - temp) + y = y1 * temp + y2 * (1 - temp) + return to_srgb(x, y, 1.0) + + +def temperature(temperature, algorithm, linear_rgb = True): + ''' + Change colour temperature according to the CIE illuminant series D + + @param temperature:float The blackbody temperature in kelvins + @param algorithm:(float)→(float, float, float) Algorithm for calculating a white point, for example `series_d` or `simple_whitepoint` + @param linear_rgb:[bool] Whether to use linear RGB, otherwise sRG is used + ''' + if temperature == 6500: + return + (r, g, b) = algorithm(temperature) + if linear_rgb: + for curve in (r_curve, g_curve, b_curve): + for i in range(i_size): + R, G, B = r_curve[i], g_curve[i], b_curve[i] + (R, G, B) = standard_to_linear(R, G, B) + r_curve[i], g_curve[i], b_curve[i] = R, G, B + rgb_brightness(r, g, b) + if linear_rgb: + for curve in (r_curve, g_curve, b_curve): + for i in range(i_size): + R, G, B = r_curve[i], g_curve[i], b_curve[i] + (R, G, B) = linear_to_standard(R, G, B) + r_curve[i], g_curve[i], b_curve[i] = R, G, B +def divide_by_maximum(): + ''' + Divide all colour components by the value of the most prominent colour component for each colour + ''' + for i in range(i_size): + m = max([abs(x) for x in (r_curve[i], g_curve[i], b_curve[i])]) + if m != 0: + for curve in (r_curve, g_curve, b_curve): + curve[i] /= m -def contrast(r, g, b): +def rgb_contrast(r, g, b): ''' - Apply contrast correction on the colour curves + Apply contrast correction on the colour curves using sRGB @param r:float The contrast parameter for the red curve @param g:float The contrast parameter for the green curve @@ -61,12 +177,23 @@ def contrast(r, g, b): ''' for (curve, level) in curves(r, g, b): if not level == 1.0: - for i in range(256): + for i in range(i_size): curve[i] = (curve[i] - 0.5) * level + 0.5 -def brightness(r, g, b): +def cie_contrast(level): ''' - Apply brightness correction on the colour curves + Apply contrast correction on the colour curves using CIE XYZ + + @param level:float The brightness parameter + ''' + if not level == 1.0: + for i in range(i_size): + (x, y, Y) = to_ciexyy(r_curve[i], g_curve[i], b_curve[i]) + (r_curve[i], g_curve[i], b_curve[i]) = to_rgb(x, y, Y * level) + +def rgb_brightness(r, g, b): + ''' + Apply brightness correction on the colour curves using sRGB @param r:float The brightness parameter for the red curve @param g:float The brightness parameter for the green curve @@ -74,9 +201,20 @@ def brightness(r, g, b): ''' for (curve, level) in curves(r, g, b): if not level == 1.0: - for i in range(256): + for i in range(i_size): curve[i] *= level +def cie_brightness(level): + ''' + Apply brightness correction on the colour curves using CIE XYZ + + @param level:float The brightness parameter + ''' + if not level == 1.0: + for i in range(i_size): + (x, y, Y) = to_ciexyy(r_curve[i], g_curve[i], b_curve[i]) + (r_curve[i], g_curve[i], b_curve[i]) = to_rgb(x, y, Y * level) + def gamma(r, g, b): ''' Apply gamma correction on the colour curves @@ -87,31 +225,54 @@ def gamma(r, g, b): ''' for (curve, level) in curves(r, g, b): if not level == 1.0: - for i in range(256): + for i in range(i_size): curve[i] **= level + +def sigmoid(r, g, b): + ''' + Apply S-curve correction on the colour curves + + @param r:float? The sigmoid parameter for the red curve + @param g:float? The sigmoid parameter for the green curve + @param b:float? The sigmoid parameter for the blue curve + ''' + for (curve, level) in curves(r, g, b): + if level is not None: + for i in range(i_size): + try: + curve[i] = 0.5 - math.log(1 / curve[i] - 1) / level + except: + curve[i] = 0; def clip(): ''' Clip all values belowed the actual minimum and above actual maximums ''' for curve in (r_curve, g_curve, b_curve): - for i in range(256): + for i in range(i_size): curve[i] = min(max(0.0, curve[i]), 1.0) -sigmoid(1.0, 1.0, 1.0) +temperature(6500, series_d, True) +divide_by_maximum() +temperature(6500, simple_whitepoint, True) clip() -contrast(1.0, 1.0, 1.0) -brightness(1.0, 1.0, 1.0) +rgb_contrast(1.0, 1.0, 1.0) +cie_contrast(1.0) +rgb_brightness(1.0, 1.0, 1.0) +cie_brightness(1.0) gamma(1.0, 1.0, 1.0) +sigmoid(None, None, None) clip() for curve in (r_curve, g_curve, b_curve): - for i in range(256): - curve[i] = int(curve[i] * 65535 + 0.5) + for i in range(i_size): + curve[i] = int(curve[i] * (o_size - 1) + 0.5) + if clip_result: + curve[i] = min(max(0, curve[i]), (o_size - 1)) print(r_curve) print(g_curve) print(b_curve) -print(Math.e) + diff --git a/src/colour.py b/src/colour.py new file mode 100644 index 0000000..df5b38d --- /dev/null +++ b/src/colour.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 + +# Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + + +def linear_to_standard(*colour): + return [12.92 * c if c <= 0.0031308 else (1 + 0.055) * c ** (1 / 2.4) - 0.055 for c in colour] + +def standard_to_linear(*colour): + return [c / 12.92 if c <= 0.04045 else ((c + 0.055) / (1 + 0.055)) ** 2.4 for c in colour] + +def ciexyy_to_ciexyz(x, y, Y): + return [Y * x / y, Y, Y * (1 - x - y) / y] + +def ciexyz_to_ciexyy(X, Y, Z): + p = -Y / X + q = 1 + Y / X + y = 1 / (p / 2 + (p ** 2 / 4 - q) ** 0.5) + x = X * y / Y + return [x, y, Y] + +def ciexyz_to_linear(X, Y, Z): + r = 3.2406 * X - 1.5372 * Y - 0.4986 * Z + g = -0.9689 * X + 1.8758 * Y + 0.0415 * Z + b = 0.0557 * X - 0.2040 * Y + 1.0570 * Z + return [r, g, b] + +def linear_to_ciexyz(r, g, b): + X = 0.4124 * r + 0.3576 * g + 0.1805 * b + Y = 0.2126 * r + 0.7152 * g + 0.0722 * b + Z = 0.0193 * r + 0.1192 * g + 1.9502 * b + return [X, Y, Z] + +def to_ciexyy(r, g, b): + (r, g, b) = standard_to_linear(r, g, b) + (X, Y, Z) = linear_to_ciexyz(r, g, b) + return ciexyz_to_ciexyy(X, Y, Z) + +def to_srgb(x, y, Y): + (X, Y, Z) = ciexyy_to_ciexyz(x, y, Y) + (r, g, b) = ciexyz_to_linear(X, Y, Z) + return linear_to_standard(r, g, b) + |