Skip to content

Commit

Permalink
HSBColor interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
McZonk committed Sep 16, 2012
1 parent 780ae7d commit 1fc6a25
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
18 changes: 18 additions & 0 deletions HSBColor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,21 @@ HSBColor::operator RGBColor() const {

return color;
}

HSBColor lerp(const HSBColor c0, const HSBColor c1, int16_t time) {
if(time < 0) {
time = 0;
} else if(time > 256) {
time = 256;
}

// a + (b - a) * t

HSBColor r;

r.h = c0.h + ((((int32_t)c1.h - (int32_t)c0.h) * time) >> 8);
r.s = c0.s + ((((int16_t)c1.s - (int16_t)c0.s) * time) >> 8);
r.b = c0.b + ((((int16_t)c1.b - (int16_t)c0.b) * time) >> 8);

return r;
}
2 changes: 2 additions & 0 deletions HSBColor.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class HSBColor {
void normalizeSelf();

operator RGBColor() const;

friend HSBColor lerp(const HSBColor c0, const HSBColor c1, const int16_t time);

public:
union {
Expand Down
8 changes: 4 additions & 4 deletions REDLEDClient.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
50AF0CC915F8D39B00BB3C5F /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 50AF0CC815F8D39B00BB3C5F /* CoreGraphics.framework */; };
50AF0CCF15F8D39B00BB3C5F /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 50AF0CCD15F8D39B00BB3C5F /* InfoPlist.strings */; };
50AF0CD115F8D39B00BB3C5F /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 50AF0CD015F8D39B00BB3C5F /* main.m */; };
50AF0CD515F8D39B00BB3C5F /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 50AF0CD415F8D39B00BB3C5F /* AppDelegate.m */; };
50AF0CD515F8D39B00BB3C5F /* AppDelegate.mm in Sources */ = {isa = PBXBuildFile; fileRef = 50AF0CD415F8D39B00BB3C5F /* AppDelegate.mm */; };
50AF0CD815F8D39B00BB3C5F /* MainStoryboard.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 50AF0CD615F8D39B00BB3C5F /* MainStoryboard.storyboard */; };
50CB647315FE4CBD001C5999 /* UIPColorSlider.m in Sources */ = {isa = PBXBuildFile; fileRef = 50CB647215FE4CBD001C5999 /* UIPColorSlider.m */; };
50CB647515FE4DEC001C5999 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 50CB647415FE4DEC001C5999 /* QuartzCore.framework */; };
Expand Down Expand Up @@ -47,7 +47,7 @@
50AF0CD015F8D39B00BB3C5F /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
50AF0CD215F8D39B00BB3C5F /* REDLEDClient-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "REDLEDClient-Prefix.pch"; sourceTree = "<group>"; };
50AF0CD315F8D39B00BB3C5F /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
50AF0CD415F8D39B00BB3C5F /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
50AF0CD415F8D39B00BB3C5F /* AppDelegate.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = AppDelegate.mm; sourceTree = "<group>"; };
50AF0CD715F8D39B00BB3C5F /* en */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = en; path = en.lproj/MainStoryboard.storyboard; sourceTree = "<group>"; };
50CB647115FE4CBD001C5999 /* UIPColorSlider.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UIPColorSlider.h; sourceTree = "<group>"; };
50CB647215FE4CBD001C5999 /* UIPColorSlider.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = UIPColorSlider.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -122,7 +122,7 @@
isa = PBXGroup;
children = (
50AF0CD315F8D39B00BB3C5F /* AppDelegate.h */,
50AF0CD415F8D39B00BB3C5F /* AppDelegate.m */,
50AF0CD415F8D39B00BB3C5F /* AppDelegate.mm */,
50AF0CD615F8D39B00BB3C5F /* MainStoryboard.storyboard */,
50A27AD715FB900600B0D39B /* RGBSCBonjourBrowserViewController.h */,
50A27AD815FB900600B0D39B /* RGBSCBonjourBrowserViewController.m */,
Expand Down Expand Up @@ -216,7 +216,7 @@
buildActionMask = 2147483647;
files = (
50AF0CD115F8D39B00BB3C5F /* main.m in Sources */,
50AF0CD515F8D39B00BB3C5F /* AppDelegate.m in Sources */,
50AF0CD515F8D39B00BB3C5F /* AppDelegate.mm in Sources */,
50AD9AD215F8DFA1005D031A /* MCUDPSocket.m in Sources */,
50A27AD915FB900600B0D39B /* RGBSCBonjourBrowserViewController.m in Sources */,
50A27ADD15FB93EA00B0D39B /* RGBSCBonjourServiceCell.m in Sources */,
Expand Down
3 changes: 2 additions & 1 deletion REDLEDClient/AppDelegate.m → REDLEDClient/AppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@

#import "AppDelegate.h"

#import "ColorListMessage.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
return YES;
}

Expand Down
20 changes: 9 additions & 11 deletions RGBStrip.ino
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <SPI.h>
#include <SPI.h>

#include "Settings.h"

Expand All @@ -25,8 +25,6 @@ LPD8806 ledStrip = LPD8806(LedStripLedCount, LedStripDataPin, LedStripClockPin);

void setup()
{
Serial.begin(9600);

ledStrip.begin();

#if defined(USE_DHCP) && (USE_DHCP > 0)
Expand Down Expand Up @@ -94,8 +92,6 @@ void loop()

case RGBStrip::ColorListMessage::Type :

Serial.println("ColorList");

void handleColorListMessage(Stream& stream);
handleColorListMessage(socket);
break;
Expand Down Expand Up @@ -170,21 +166,23 @@ void handleColorListMessage(Stream& stream) {
RGBStrip::ColorListMessage::Key pKey;
stream.readBytes((char*)&pKey, sizeof(pKey));

Serial.println(pKey.index, DEC);

for(int i = 1; i < count; ++i) {
RGBStrip::ColorListMessage::Key cKey;
stream.readBytes((char*)&cKey, sizeof(cKey));

Serial.println(cKey.index, DEC);

RGBColor rgbColor = pKey.color;
int stepCount = cKey.index - pKey.index;
int stepIndex = 0;

for(int j = pKey.index; j <= cKey.index; ++j) {
for(int j = pKey.index; j <= cKey.index; ++j, ++stepIndex) {
if(offset + j >= LedStripLedCount) {
break;
}

int time = ((stepIndex << 8) / stepCount);

HSBColor hsbColor = lerp(pKey.color, cKey.color, time);
RGBColor rgbColor = hsbColor;

ledStrip.setPixelColor(offset + j, GammaCorretion(rgbColor.r), GammaCorretion(rgbColor.g), GammaCorretion(rgbColor.b));
}

Expand Down

0 comments on commit 1fc6a25

Please sign in to comment.