Skip to content

Commit

Permalink
Send HSBColor
Browse files Browse the repository at this point in the history
  • Loading branch information
McZonk committed Sep 13, 2012
1 parent 37f1f13 commit f4a1d9d
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 179 deletions.
96 changes: 44 additions & 52 deletions HSBColor.cpp
Original file line number Diff line number Diff line change
@@ -1,62 +1,54 @@
#include "HSBColor.h"

const HSBColor HSBColor::Red(0, 255, 255);
const HSBColor HSBColor::Yellow(256, 255, 255);
const HSBColor HSBColor::Green(512, 255, 255);
const HSBColor HSBColor::Cyan(768, 255, 255);
const HSBColor HSBColor::Blue(1024, 255, 255);
const HSBColor HSBColor::Magenta(1280, 255, 255);


HSBColor::operator RGBColor() const {
const uint8_t hh = h >> 8;
const uint8_t lh = h & 0xff;
const uint8_t hh = h >> 8;
const uint8_t lh = h & 0xff;

RGBColor color;
switch(hh) {
case 0:
color.r = 255;
color.g = lh;
color.b = 0;
break;
case 1:
color.r = 255 - lh;
color.g = 255;
color.b = 0;
break;
case 2:
color.r = 0;
color.g = 255;
color.b = lh;
break;
case 3:
color.r = 0;
color.g = 255 - lh;
color.b = 255;
break;
case 4:
color.r = lh;
color.g = 0;
color.b = 255;
break;
default:
color.r = 255;
color.g = 0;
color.b = 255 - lh;
break;
}
RGBColor color;
switch(hh) {
case 0:
color.r = 255;
color.g = lh;
color.b = 0;
break;
case 1:
color.r = 255 - lh;
color.g = 255;
color.b = 0;
break;
case 2:
color.r = 0;
color.g = 255;
color.b = lh;
break;
case 3:
color.r = 0;
color.g = 255 - lh;
color.b = 255;
break;
case 4:
color.r = lh;
color.g = 0;
color.b = 255;
break;
default:
color.r = 255;
color.g = 0;
color.b = 255 - lh;
break;
}

const uint16_t saturation1 = s + 1;
const uint16_t saturation1 = s + 1;

color.r = 255 - (((255 - color.r) * saturation1) >> 8);
color.g = 255 - (((255 - color.g) * saturation1) >> 8);
color.b = 255 - (((255 - color.b) * saturation1) >> 8);
color.r = 255 - (((255 - color.r) * saturation1) >> 8);
color.g = 255 - (((255 - color.g) * saturation1) >> 8);
color.b = 255 - (((255 - color.b) * saturation1) >> 8);

const uint16_t brightness1 = b + 1;
const uint16_t brightness1 = b + 1;

color.r = (color.r * brightness1) >> 8;
color.g = (color.g * brightness1) >> 8;
color.b = (color.b * brightness1) >> 8;
color.r = (color.r * brightness1) >> 8;
color.g = (color.g * brightness1) >> 8;
color.b = (color.b * brightness1) >> 8;

return color;
return color;
}
55 changes: 27 additions & 28 deletions HSBColor.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,44 @@

class HSBColor {
public:
static const HSBColor Red;
static const HSBColor Yellow;
static const HSBColor Green;
static const HSBColor Cyan;
static const HSBColor Blue;
static const HSBColor Magenta;
HSBColor();

HSBColor();
HSBColor(int16_t h, uint8_t s, uint8_t b);
HSBColor(float h, float s, float b);

void normalizeSelf();

HSBColor(int16_t h, uint8_t s, uint8_t v);

void normalizeSelf();

operator RGBColor() const;
operator RGBColor() const;

public:
union {
struct {
int16_t h;
uint8_t s;
uint8_t b;
};
uint32_t value;
};
union {
struct {
int16_t h;
uint8_t s;
uint8_t b;
};
uint32_t value;
};
};

inline HSBColor::HSBColor() {
}

inline HSBColor::HSBColor(int16_t h, uint8_t s, uint8_t b) :
h(h),
s(s),
b(b) {
h(h),
s(s),
b(b) {
}

inline void HSBColor::normalizeSelf() {
h %= HSBColorHueRange;
if(h < 0) {
h += HSBColorHueRange;
}
inline HSBColor::HSBColor(float h, float s, float b) :
h(h * (HSBColorHueRange - 1.0f)),
s(s * 255.0f),
b(b * 255.0f) {
}

inline void HSBColor::normalizeSelf() {
h %= HSBColorHueRange;
if(h < 0) {
h += HSBColorHueRange;
}
}
22 changes: 21 additions & 1 deletion REDLEDClient.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
50288CAD16025874005F6F3A /* HSBColor.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 50288CA916025874005F6F3A /* HSBColor.cpp */; };
50288CAE16025874005F6F3A /* RGBColor.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 50288CAB16025874005F6F3A /* RGBColor.cpp */; };
50A27AD915FB900600B0D39B /* RGBSCBonjourBrowserViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 50A27AD815FB900600B0D39B /* RGBSCBonjourBrowserViewController.m */; };
50A27ADD15FB93EA00B0D39B /* RGBSCBonjourServiceCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 50A27ADC15FB93EA00B0D39B /* RGBSCBonjourServiceCell.m */; };
50A27AE015FB9B2800B0D39B /* RGBSCViewController.mm in Sources */ = {isa = PBXBuildFile; fileRef = 50A27ADF15FB9B2800B0D39B /* RGBSCViewController.mm */; };
Expand All @@ -24,6 +26,10 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
50288CA916025874005F6F3A /* HSBColor.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = HSBColor.cpp; sourceTree = "<group>"; };
50288CAA16025874005F6F3A /* HSBColor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HSBColor.h; sourceTree = "<group>"; };
50288CAB16025874005F6F3A /* RGBColor.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RGBColor.cpp; sourceTree = "<group>"; };
50288CAC16025874005F6F3A /* RGBColor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RGBColor.h; sourceTree = "<group>"; };
50A27AD715FB900600B0D39B /* RGBSCBonjourBrowserViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RGBSCBonjourBrowserViewController.h; sourceTree = "<group>"; };
50A27AD815FB900600B0D39B /* RGBSCBonjourBrowserViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RGBSCBonjourBrowserViewController.m; sourceTree = "<group>"; };
50A27ADB15FB93EA00B0D39B /* RGBSCBonjourServiceCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RGBSCBonjourServiceCell.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -65,10 +71,22 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
50AF0CB515F8D39B00BB3C5F = {
50288CB01602587E005F6F3A /* Shared */ = {
isa = PBXGroup;
children = (
50AD9ACF15F8DF98005D031A /* RGBStrip.h */,
50288CAC16025874005F6F3A /* RGBColor.h */,
50288CAB16025874005F6F3A /* RGBColor.cpp */,
50288CAA16025874005F6F3A /* HSBColor.h */,
50288CA916025874005F6F3A /* HSBColor.cpp */,
);
name = Shared;
sourceTree = "<group>";
};
50AF0CB515F8D39B00BB3C5F = {
isa = PBXGroup;
children = (
50288CB01602587E005F6F3A /* Shared */,
50AF0CCA15F8D39B00BB3C5F /* REDLEDClient */,
50AF0CC315F8D39B00BB3C5F /* Frameworks */,
50AF0CC115F8D39B00BB3C5F /* Products */,
Expand Down Expand Up @@ -198,6 +216,8 @@
50A27ADD15FB93EA00B0D39B /* RGBSCBonjourServiceCell.m in Sources */,
50A27AE015FB9B2800B0D39B /* RGBSCViewController.mm in Sources */,
50CB647315FE4CBD001C5999 /* UIPColorSlider.m in Sources */,
50288CAD16025874005F6F3A /* HSBColor.cpp in Sources */,
50288CAE16025874005F6F3A /* RGBColor.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
9 changes: 9 additions & 0 deletions REDLEDClient/RGBSCViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@
@property (nonatomic, strong) IBOutlet UIPColorSlider* sSlider;
@property (nonatomic, strong) IBOutlet UIPColorSlider* bSlider;

@property (nonatomic, strong) IBOutlet UIStepper* firstStepper;
@property (nonatomic, strong) IBOutlet UIStepper* lastStepper;

@property (nonatomic, strong) IBOutlet UILabel* firstTextView;
@property (nonatomic, strong) IBOutlet UILabel* lastTextView;

- (IBAction)sliderChanged:(id)sender;

- (IBAction)firstLEDChanged:(id)sender;
- (IBAction)lastLEDChanged:(id)sender;

@end
49 changes: 23 additions & 26 deletions REDLEDClient/RGBSCViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "MCUDPSocket.h"

#import "RGBStrip.h"
#import "HSBColor.h"

@interface RGBSCViewController () <NSStreamDelegate>

Expand Down Expand Up @@ -92,41 +93,37 @@ - (IBAction)sliderChanged:(id)sender
[UIColor colorWithHue:h saturation:s brightness:1.0f alpha:1.0f],
];

UIColor* color = [UIColor colorWithHue:h saturation:s brightness:b alpha:1.0f];

#if 1
float colorf[3];

[color getRed:&colorf[0] green:&colorf[1] blue:&colorf[2] alpha:NULL];

unsigned char colorb[3];

for(NSUInteger i = 0; i < 3; ++i)
{
colorb[i] = (unsigned char)(colorf[i] * 255.0f);
if(colorb[i] < 2)
{
colorb[i] = 2;
}
}

//NSLog(@"%02x %02x %02x", colorb[0], colorb[1], colorb[2]);

struct RGBStripMessageSetAll message;
message.header.identifier = RGBStripMessageIdentifier;
message.header.type = RGBStripMessageTypeSetAll;
message.header.length = sizeof(message);
HSBColor color = HSBColor(h, s, b);

RGBStripMessageSetRange message;
message.hue = color.h;
message.saturation = color.s;
message.brightness = color.b;

message.r = colorb[0];
message.g = colorb[1];
message.b = colorb[2];
message.firstLED = self.firstStepper.value;
message.lastLED = self.lastStepper.value;

message.animation = 0;

message.header.checksum = RGBStripCalcChecksum(&message);
message.fillChecksum();

NSData* data = RGBStripMessageToNSData(&message);
NSData* data = message;

NSLog(@"%@ success:%d", data, [self.socket sendData:data]);
#endif
}

- (IBAction)firstLEDChanged:(id)sender
{
self.firstTextView.text = [NSString stringWithFormat:@"%.0f", self.firstStepper.value];
}

- (IBAction)lastLEDChanged:(id)sender
{
self.lastTextView.text = [NSString stringWithFormat:@"%.0f", self.lastStepper.value];
}

@end
Loading

0 comments on commit f4a1d9d

Please sign in to comment.