| | 602 | |
|---|
| | 603 | MochiKit.Color |
|---|
| | 604 | ============== |
|---|
| | 605 | |
|---|
| | 606 | * Full CSS3 color model with alpha |
|---|
| | 607 | * NSColor-like API (from Cocoa) |
|---|
| | 608 | * Works in RGB, HSV, HSL |
|---|
| | 609 | * Normalized, all components from [0.0, 1.0] |
|---|
| | 610 | |
|---|
| | 611 | |
|---|
| | 612 | Creating colors from components |
|---|
| | 613 | =============================== |
|---|
| | 614 | |
|---|
| | 615 | * Color.fromRGB(r, g, b, alpha=1.0) |
|---|
| | 616 | * Color.fromHSL(h, s, l, alpha=1.0) |
|---|
| | 617 | * Color.fromHSV(h, s, v, alpha=1.0) |
|---|
| | 618 | |
|---|
| | 619 | Will also take objects, e.g. {r: 1, g: 0, b: 0, a: 1} |
|---|
| | 620 | |
|---|
| | 621 | |
|---|
| | 622 | Creating colors from strings |
|---|
| | 623 | ============================ |
|---|
| | 624 | |
|---|
| | 625 | Color.fromString(str): |
|---|
| | 626 | Generally "does what you mean" with any valid CSS color description. |
|---|
| | 627 | "rgb(...)", "hsl(...)", "#RRGGBB", "blue" |
|---|
| | 628 | |
|---|
| | 629 | Implementations in fromRGBString, fromHSLString, fromHexString, fromName. |
|---|
| | 630 | |
|---|
| | 631 | |
|---|
| | 632 | Creating colors from DOM elements |
|---|
| | 633 | ================================= |
|---|
| | 634 | |
|---|
| | 635 | Color.fromBackground(node) |
|---|
| | 636 | Color.fromComputedStyle(node, style, css) |
|---|
| | 637 | Color.fromText(node) |
|---|
| | 638 | |
|---|
| | 639 | |
|---|
| | 640 | Built-in NSColor colors |
|---|
| | 641 | ======================= |
|---|
| | 642 | |
|---|
| | 643 | NSColor-based constructors for basic colors |
|---|
| | 644 | |
|---|
| | 645 | - Color.whiteColor() |
|---|
| | 646 | - Color.blueColor() |
|---|
| | 647 | - Color.transparentColor() |
|---|
| | 648 | - ... |
|---|
| | 649 | |
|---|
| | 650 | |
|---|
| | 651 | Mixing colors |
|---|
| | 652 | ============= |
|---|
| | 653 | |
|---|
| | 654 | - color.blendedColor(otherColor, fraction) |
|---|
| | 655 | - color.colorWithHue(hue) |
|---|
| | 656 | - color.colorWithLevel(level) |
|---|
| | 657 | - color.colorWithSaturation(saturation) |
|---|
| | 658 | - color.colorWithAlpha(alpha) |
|---|
| | 659 | |
|---|
| | 660 | |
|---|
| | 661 | Getting color values |
|---|
| | 662 | ==================== |
|---|
| | 663 | |
|---|
| | 664 | Objects: |
|---|
| | 665 | |
|---|
| | 666 | - color.asRGB() |
|---|
| | 667 | - color.asHSL() |
|---|
| | 668 | - color.asHSV() |
|---|
| | 669 | |
|---|
| | 670 | Strings: |
|---|
| | 671 | |
|---|
| | 672 | - color.toHexString() |
|---|
| | 673 | - color.toRGBString() |
|---|
| | 674 | - color.toHSLString() |
|---|
| | 675 | |
|---|
| | 676 | |
|---|
| | 677 | MochiKit.Async |
|---|
| | 678 | ============== |
|---|
| | 679 | |
|---|
| | 680 | * Yes, there is AJAX |
|---|
| | 681 | * Generalized asynchronous model based on Twisted |
|---|
| | 682 | * Handles XMLHttpRequest and timed events (setTimeout) |
|---|
| | 683 | |
|---|
| | 684 | |
|---|
| | 685 | WTF is a Deferred? |
|---|
| | 686 | ================== |
|---|
| | 687 | |
|---|
| | 688 | * A "promise" to call back exactly once with a single result (or error) |
|---|
| | 689 | at some time (could be immediately!) |
|---|
| | 690 | * Can be chained |
|---|
| | 691 | * Model works in any asynchronous platform with nearly any language |
|---|
| | 692 | * Not "ideal" API, but without threads or anything resembling coroutines |
|---|
| | 693 | it's the best you can do. |
|---|
| | 694 | |
|---|
| | 695 | |
|---|
| | 696 | Trivial Deferreds |
|---|
| | 697 | ================= |
|---|
| | 698 | |
|---|
| | 699 | succeed(value): |
|---|
| | 700 | A successful deferred that will callback with value |
|---|
| | 701 | |
|---|
| | 702 | fail(error): |
|---|
| | 703 | A failed deferred that will errback with value |
|---|
| | 704 | |
|---|
| | 705 | maybeDeferred(func, arguments..): |
|---|
| | 706 | Call a function and make sure it returns a Deferred. Non-deferred return |
|---|
| | 707 | values get wrapped with succeed, and errors get wrapped with fail. |
|---|
| | 708 | |
|---|
| | 709 | |
|---|
| | 710 | Timed Events |
|---|
| | 711 | ============ |
|---|
| | 712 | |
|---|
| | 713 | wait(seconds, value): |
|---|
| | 714 | Return a Deferred that calls back with value after seconds have passed |
|---|
| | 715 | |
|---|
| | 716 | callLater(seconds, func, arguments...): |
|---|
| | 717 | Return a Deferred that will call func(arguments) after seconds have passed |
|---|
| | 718 | |
|---|
| | 719 | |
|---|
| | 720 | Network Events |
|---|
| | 721 | ============== |
|---|
| | 722 | |
|---|
| | 723 | doSimpleXMLHttpRequest(url[, queryArguments]): |
|---|
| | 724 | Set up a GET XMLHttpRequest to url and return a Deferred. The Deferred |
|---|
| | 725 | will callback with the XMLHttpRequest instance. |
|---|
| | 726 | |
|---|
| | 727 | loadJSONDoc(url[, queryArguments]): |
|---|
| | 728 | Set up a simple XMLHttpRequest, process its responseText as JSON, and |
|---|
| | 729 | callback with the JSON object. |
|---|
| | 730 | |
|---|
| | 731 | |
|---|
| | 732 | Deferred Usage |
|---|
| | 733 | ============== |
|---|
| | 734 | |
|---|
| | 735 | Fetch a JSON document:: |
|---|
| | 736 | |
|---|
| | 737 | function gotDocument(json) { |
|---|
| | 738 | // ... |
|---|
| | 739 | } |
|---|
| | 740 | var d = loadJSONDoc("example.json"); |
|---|
| | 741 | d.addCallback(gotDocument); |
|---|
| | 742 | d.addErrback(logError); |
|---|
| | 743 | |
|---|
| | 744 | Result Chaining |
|---|
| | 745 | =============== |
|---|
| | 746 | |
|---|
| | 747 | The implementation of loadJSONDoc looks like this:: |
|---|
| | 748 | |
|---|
| | 749 | var d = doSimpleXMLHttpRequest(url); |
|---|
| | 750 | d.addCallback(evalJSONDoc); |
|---|
| | 751 | return d; |
|---|
| | 752 | |
|---|
| | 753 | Further callbacks get the result of evalJSONDoc, not the original |
|---|
| | 754 | doSimpleXMLHttpRequest result. |
|---|
| | 755 | |
|---|
| | 756 | |
|---|
| | 757 | Deferred Chaining |
|---|
| | 758 | ================= |
|---|
| | 759 | |
|---|
| | 760 | Returning a Deferred from a callback will "pause" the callback chain:: |
|---|
| | 761 | |
|---|
| | 762 | function gotDocument(json) { |
|---|
| | 763 | // ... |
|---|
| | 764 | } |
|---|
| | 765 | function delay(res) { |
|---|
| | 766 | return wait(2.0, res); |
|---|
| | 767 | } |
|---|
| | 768 | var d = loadJSONDoc("example.json"); |
|---|
| | 769 | d.addCallback(delay); |
|---|
| | 770 | d.addCallback(gotDocument); |
|---|
| | 771 | d.addErrback(logError); |
|---|
| | 772 | |
|---|