Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ColorLib.h@0:dff187a80020, 2016-07-26 (annotated)
- Committer:
- mutech
- Date:
- Tue Jul 26 18:09:24 2016 +0000
- Revision:
- 0:dff187a80020
- Child:
- 1:0288c920499c
WS2811/WS2812 Class Library
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mutech | 0:dff187a80020 | 1 | /* ColorLib.h |
| mutech | 0:dff187a80020 | 2 | * mbed Microcontroller Library |
| mutech | 0:dff187a80020 | 3 | * Copyright (c) 2016 muetch, t.kuroki, MIT License |
| mutech | 0:dff187a80020 | 4 | * |
| mutech | 0:dff187a80020 | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
| mutech | 0:dff187a80020 | 6 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
| mutech | 0:dff187a80020 | 7 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
| mutech | 0:dff187a80020 | 8 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
| mutech | 0:dff187a80020 | 9 | * furnished to do so, subject to the following conditions: |
| mutech | 0:dff187a80020 | 10 | * |
| mutech | 0:dff187a80020 | 11 | * The above copyright notice and this permission notice shall be included in all copies or |
| mutech | 0:dff187a80020 | 12 | * substantial portions of the Software. |
| mutech | 0:dff187a80020 | 13 | * |
| mutech | 0:dff187a80020 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
| mutech | 0:dff187a80020 | 15 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| mutech | 0:dff187a80020 | 16 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| mutech | 0:dff187a80020 | 17 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| mutech | 0:dff187a80020 | 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| mutech | 0:dff187a80020 | 19 | */ |
| mutech | 0:dff187a80020 | 20 | |
| mutech | 0:dff187a80020 | 21 | #pragma once |
| mutech | 0:dff187a80020 | 22 | |
| mutech | 0:dff187a80020 | 23 | #ifndef COLORLIB_H |
| mutech | 0:dff187a80020 | 24 | #define COLORLIB_H |
| mutech | 0:dff187a80020 | 25 | |
| mutech | 0:dff187a80020 | 26 | #include <stdint.h> |
| mutech | 0:dff187a80020 | 27 | |
| mutech | 0:dff187a80020 | 28 | //---------------------------------------------------------------------------- |
| mutech | 0:dff187a80020 | 29 | #define GetRValue(c) ((uint8_t)(c)) |
| mutech | 0:dff187a80020 | 30 | #define GetGValue(c) ((uint8_t)((c) >> 8)) |
| mutech | 0:dff187a80020 | 31 | #define GetBValue(c) ((uint8_t)((c) >> 16)) |
| mutech | 0:dff187a80020 | 32 | #define COLORREF(r, g ,b) ((uint32_t)((uint8_t)(r) | ((uint8_t)(g) << 8) | ((uint8_t)(b) << 16))) |
| mutech | 0:dff187a80020 | 33 | |
| mutech | 0:dff187a80020 | 34 | //---------------------------------------------------------------------------- |
| mutech | 0:dff187a80020 | 35 | /** |
| mutech | 0:dff187a80020 | 36 | RGB Color |
| mutech | 0:dff187a80020 | 37 | */ |
| mutech | 0:dff187a80020 | 38 | struct RGBColor |
| mutech | 0:dff187a80020 | 39 | { |
| mutech | 0:dff187a80020 | 40 | uint8_t red; |
| mutech | 0:dff187a80020 | 41 | uint8_t green; |
| mutech | 0:dff187a80020 | 42 | uint8_t blue; |
| mutech | 0:dff187a80020 | 43 | |
| mutech | 0:dff187a80020 | 44 | /** |
| mutech | 0:dff187a80020 | 45 | Constructor with rgb initializing |
| mutech | 0:dff187a80020 | 46 | |
| mutech | 0:dff187a80020 | 47 | @param r - the red byte |
| mutech | 0:dff187a80020 | 48 | @param g - the green byte |
| mutech | 0:dff187a80020 | 49 | @param b - the blue byte |
| mutech | 0:dff187a80020 | 50 | */ |
| mutech | 0:dff187a80020 | 51 | RGBColor(uint8_t r, uint8_t g, uint8_t b) |
| mutech | 0:dff187a80020 | 52 | { |
| mutech | 0:dff187a80020 | 53 | red = r; |
| mutech | 0:dff187a80020 | 54 | green = g; |
| mutech | 0:dff187a80020 | 55 | blue = b; |
| mutech | 0:dff187a80020 | 56 | } |
| mutech | 0:dff187a80020 | 57 | |
| mutech | 0:dff187a80020 | 58 | RGBColor(uint32_t rgb) |
| mutech | 0:dff187a80020 | 59 | { |
| mutech | 0:dff187a80020 | 60 | red = GetRValue(rgb); |
| mutech | 0:dff187a80020 | 61 | green = GetGValue(rgb); |
| mutech | 0:dff187a80020 | 62 | blue = GetBValue(rgb); |
| mutech | 0:dff187a80020 | 63 | } |
| mutech | 0:dff187a80020 | 64 | |
| mutech | 0:dff187a80020 | 65 | RGBColor(int rgb) |
| mutech | 0:dff187a80020 | 66 | { |
| mutech | 0:dff187a80020 | 67 | red = GetRValue(rgb); |
| mutech | 0:dff187a80020 | 68 | green = GetGValue(rgb); |
| mutech | 0:dff187a80020 | 69 | blue = GetBValue(rgb); |
| mutech | 0:dff187a80020 | 70 | } |
| mutech | 0:dff187a80020 | 71 | |
| mutech | 0:dff187a80020 | 72 | /** |
| mutech | 0:dff187a80020 | 73 | Default constructor |
| mutech | 0:dff187a80020 | 74 | */ |
| mutech | 0:dff187a80020 | 75 | RGBColor() {} |
| mutech | 0:dff187a80020 | 76 | |
| mutech | 0:dff187a80020 | 77 | // allow copy construction |
| mutech | 0:dff187a80020 | 78 | inline RGBColor(const RGBColor& rhs) |
| mutech | 0:dff187a80020 | 79 | { |
| mutech | 0:dff187a80020 | 80 | red = rhs.red; |
| mutech | 0:dff187a80020 | 81 | green = rhs.green; |
| mutech | 0:dff187a80020 | 82 | blue = rhs.blue; |
| mutech | 0:dff187a80020 | 83 | } |
| mutech | 0:dff187a80020 | 84 | |
| mutech | 0:dff187a80020 | 85 | // allow assignment from one RGB struct to another |
| mutech | 0:dff187a80020 | 86 | inline RGBColor& operator= (const RGBColor& rhs) |
| mutech | 0:dff187a80020 | 87 | { |
| mutech | 0:dff187a80020 | 88 | red = rhs.red; |
| mutech | 0:dff187a80020 | 89 | green = rhs.green; |
| mutech | 0:dff187a80020 | 90 | blue = rhs.blue; |
| mutech | 0:dff187a80020 | 91 | return *this; |
| mutech | 0:dff187a80020 | 92 | } |
| mutech | 0:dff187a80020 | 93 | |
| mutech | 0:dff187a80020 | 94 | inline RGBColor& operator= (const uint32_t rgb) |
| mutech | 0:dff187a80020 | 95 | { |
| mutech | 0:dff187a80020 | 96 | red = GetRValue(rgb); |
| mutech | 0:dff187a80020 | 97 | green = GetGValue(rgb); |
| mutech | 0:dff187a80020 | 98 | blue = GetBValue(rgb); |
| mutech | 0:dff187a80020 | 99 | return *this; |
| mutech | 0:dff187a80020 | 100 | } |
| mutech | 0:dff187a80020 | 101 | |
| mutech | 0:dff187a80020 | 102 | inline RGBColor& operator= (const int rgb) |
| mutech | 0:dff187a80020 | 103 | { |
| mutech | 0:dff187a80020 | 104 | red = GetRValue(rgb); |
| mutech | 0:dff187a80020 | 105 | green = GetGValue(rgb); |
| mutech | 0:dff187a80020 | 106 | blue = GetBValue(rgb); |
| mutech | 0:dff187a80020 | 107 | return *this; |
| mutech | 0:dff187a80020 | 108 | } |
| mutech | 0:dff187a80020 | 109 | |
| mutech | 0:dff187a80020 | 110 | operator uint32_t() |
| mutech | 0:dff187a80020 | 111 | { |
| mutech | 0:dff187a80020 | 112 | return COLORREF(red, green, blue); |
| mutech | 0:dff187a80020 | 113 | } |
| mutech | 0:dff187a80020 | 114 | |
| mutech | 0:dff187a80020 | 115 | operator int() |
| mutech | 0:dff187a80020 | 116 | { |
| mutech | 0:dff187a80020 | 117 | return COLORREF(red, green, blue); |
| mutech | 0:dff187a80020 | 118 | } |
| mutech | 0:dff187a80020 | 119 | }; |
| mutech | 0:dff187a80020 | 120 | |
| mutech | 0:dff187a80020 | 121 | //---------------------------------------------------------------------------- |
| mutech | 0:dff187a80020 | 122 | #endif // end of COLORLIB_H |