rgb Dialog::hslToRgb(hsl in) { rgb out; double h,s,l; double r, g, b; h=in.h; s=in.s; l=in.l; if(s == 0){ r = g = b = l; // achromatic } else { double q = l < 0.5 ? l * (1 + s) : l + s - l * s; double p = 2 * l - q; r = hue2rgb(p, q, h + 1/3); g = hue2rgb(p, q, h); b = hue2rgb(p, q, h - 1/3); } out.r = r*255; out.g = g*255; out.b = b*255; return out; }
/** hue=0-360; sat=0-1; lum=0-1 **/ vector<int> Hsl::hsl2rgb(double hue, double sat, double lum) { double RGB[3]; vector<int> results(3,0); if(sat==0) { RGB[0] = round(lum * 255); RGB[1] = round(lum * 255); RGB[2] = round(lum * 255); } else { double temp1, temp2; if(lum<0.5) temp1 = lum*(1+sat); else temp1 = (lum+sat) - (lum*sat); temp2 = (2*lum) - temp1; hue /= 360; RGB[0] = round(255*hue2rgb(temp2,temp1,(hue+(1./3.)))); RGB[1] = round(255*hue2rgb(temp2,temp1,hue)); RGB[2] = round(255*hue2rgb(temp2,temp1,(hue-(1./3.)))); } results[0] = (int)RGB[0]; results[1] = (int)RGB[1]; results[2] = (int)RGB[2]; return results; }
void hsl2rgb(float rgb[3],float h,float s,float l) { float m1,m2; if( s==0) { rgb[0]=rgb[1]=rgb[2]=l; return; } m2=l<0.5?l*(1.0+s):l+s-l*s; m1=(2.0*l-m2); rgb[0] = hue2rgb(m1,m2,h + (1.0/3.0)); rgb[1] = hue2rgb(m1,m2,h); rgb[2] = hue2rgb(m1,m2,h - (1.0/3.0)); }
unsigned int hsl2rgb(float h, float s, float l) { float m1,m2; int r,g,b; m2 = l < 0.5f ? l * (s+1.0f) : (l+s) - (l*s); m1 = l*2.0f-m2; r = (int)(255.0f * hue2rgb(m1,m2,h+(1.0f/3.0f))); g = (int)(255.0f * hue2rgb(m1,m2,h)); b = (int)(255.0f * hue2rgb(m1,m2,h-(1.0f/3.0f))); return (0xff << 24) | (b << 16) | (g << 8) | r; }
void hslToRgb(float h, float s, float l, float& r, float& g, float& b) { if(s == 0) { r = g = b = l; // achromatic } else { float q = l < 0.5 ? l * (1.0 + s) : l + s - l * s; float p = 2.0 * l - q; r = hue2rgb(p, q, h + 1.0/3.0); g = hue2rgb(p, q, h); b = hue2rgb(p, q, h - 1.0/3.0); } }
void xavix_state::update_pen(int pen, uint8_t shval, uint8_t lval) { uint16_t dat; dat = shval; dat |= lval << 8; int l_raw = (dat & 0x1f00) >> 8; int s_raw = (dat & 0x00e0) >> 5; int h_raw = (dat & 0x001f) >> 0; //if (h_raw > 24) // LOG("hraw >24 (%02x)\n", h_raw); //if (l_raw > 24) // LOG("lraw >24 (%02x)\n", l_raw); //if (s_raw > 7) // LOG("s_raw >5 (%02x)\n", s_raw); double l = (double)l_raw / 24.0f; // ekara and drgqst go up to 23 during fades, expect that to be brightest l = l * (std::atan(1)*2); // does not appear to be a linear curve l = std::sin(l); double s = (double)s_raw / 7.0f; s = s * (std::atan(1)*2); // does not appear to be a linear curve s = std::sin(s); double h = (double)h_raw / 24.0f; // hue values 24-31 render as transparent double r, g, b; if (s == 0) { r = g = b = l; // greyscale } else { double q = l < 0.5f ? l * (1 + s) : l + s - l * s; double p = 2 * l - q; r = hue2rgb(p, q, h + 1 / 3.0f); g = hue2rgb(p, q, h); b = hue2rgb(p, q, h - 1 / 3.0f); } int r_real = r * 255.0f; int g_real = g * 255.0f; int b_real = b * 255.0f; m_palette->set_pen_color(pen, r_real, g_real, b_real); }
void hsl2rgb(struct razer_hsl *hsl,struct razer_rgb *rgb) { if(hsl->s==0.0f) { rgb->r = (unsigned char)(hsl->l*255.0f); rgb->g = (unsigned char)(hsl->l*255.0f); rgb->b = (unsigned char)(hsl->l*255.0f); return; } float q,p; if(hsl->l < 0.5f) q = hsl->l*(1.0f+hsl->s); else q = hsl->l + hsl->s - hsl->l * hsl->s; p = 2.0f * hsl->l - q; rgb->r = (unsigned char)(hue2rgb(p,q,hsl->h+(1.0f/3.0f))*255.0f); rgb->g = (unsigned char)(hue2rgb(p,q,hsl->h)*255.0f); rgb->b = (unsigned char)(hue2rgb(p,q,hsl->h-(1.0f/3.0f))*255.0f); }
Color& Color::setHSL( float h, float s, float l ) { if ( s == 0.f ) { r = g = b = l; } else { auto p = l <= 0.5f ? l * ( 1.f + s ) : l + s - ( l * s ); auto q = ( 2.f * l ) - p; r = hue2rgb( q, p, h + 1.f / 3.f ); g = hue2rgb( q, p, h ); b = hue2rgb( q, p, h - 1.f / 3.f ); } return *this; }
static uint8_t NormalState_handleIR( uint8_t cmdCode ) { #if (MONO_COLOR_CLOCK != 1) if( UI_NORMAL_MODE == cmdCode){ ++(g_params->curColorProfile); g_params->curColorProfile %= UI_COLOR_PRESET_COUNT; NormalState_enter((void*)1); }else if( UI_CHANGE_R == cmdCode ){ log_state("CR\n"); mode_normalState.colorToSet = 0; }else if( UI_CHANGE_G == cmdCode ){ log_state("CG\n"); mode_normalState.colorToSet = 1; }else if( UI_CHANGE_B == cmdCode ){ log_state("CB\n"); mode_normalState.colorToSet = 2; }else if( UI_CHANGE_HUE == cmdCode ){ log_state("CH\n"); mode_normalState.colorToSet = 4; }else if( UI_UP == cmdCode || UI_DOWN == cmdCode) { log_state("CC\n"); int8_t dir = UI_UP == cmdCode?1:-1; if( mode_normalState.colorToSet < 4 ){ // handle RGB-Changes uint8_t* rgb = (uint8_t*)(&g_params->colorPresets[ g_params->curColorProfile ]); incDecRange(&rgb[ mode_normalState.colorToSet ], dir,0, MAX_PWM_STEPS-1); pwm_set_color_step( rgb[0], rgb[1], rgb[2] ); }else{ // handle HUE-Changes uint8_t r,g,b; if( dir<0 && mode_normalState.curHue < HUE_MANUAL_STEPS ){ mode_normalState.curHue = HUE_MAX; }else if( dir>0 && mode_normalState.curHue >= HUE_MAX-HUE_MANUAL_STEPS){ mode_normalState.curHue = 0; }else{ mode_normalState.curHue += dir*HUE_MANUAL_STEPS; } hue2rgb(mode_normalState.curHue, &r, &g, &b); SetPWMs(r,g,b); } }else #endif { return FALSE; } return TRUE; }
void ChainableLED::setColorHSB(byte led, float hue, float saturation, float brightness) { float r, g, b; constrain(hue, 0.0, 1.0); constrain(saturation, 0.0, 1.0); constrain(brightness, 0.0, 1.0); if(saturation == 0.0) { r = g = b = brightness; } else { float q = brightness < 0.5 ? brightness * (1.0 + saturation) : brightness + saturation - brightness * saturation; float p = 2.0 * brightness - q; r = hue2rgb(p, q, hue + 1.0/3.0); g = hue2rgb(p, q, hue); b = hue2rgb(p, q, hue - 1.0/3.0); } setColorRGB(led, (byte)(255.0*r), (byte)(255.0*g), (byte)(255.0*b)); }
void render(int w, int h, int nFuncs, TransformFunc *funcs, int num) { Color **hist = malloc(w * sizeof(Color*)); for (int x = 0; x < w; ++x) { hist[x] = malloc(h * sizeof(Color)); for (int y = 0; y < h; ++y) { hist[x][y] = (Color) {0.0, 0.0, 0.0}; } } float maxR = 0, maxG = 0, maxB = 0; Point p = {0.5, 0.5}; float color = 0.5; for (unsigned long long i = 0; i < ((unsigned long long)(w) * (unsigned long long)(w) * (unsigned long long)(h) * (unsigned long long)(h) / 10000ULL); ++i) { int affIdx = rand() % sizeof(affines) / sizeof(float[6]); affine(&p, affines[affIdx]); int idx = rand() % nFuncs; funcs[idx](&p, affines[affIdx]); int x = (int)((p.x + 1) * (w / 2)); int y = (int)((p.y + 1) * (h / 2)); color = (color + ((float)idx / nFuncs)) / 2; if (x >= 0 && x < w && y >= 0 && y < h) { // do NOT try to "optimize" this by creating a 'rgb' local variable // for some reason that makes it slower hist[x][y].r += hue2rgb(color).r; if (hist[x][y].r > maxR) maxR = hist[x][y].r; hist[x][y].g += hue2rgb(color).g; if (hist[x][y].g > maxG) maxG = hist[x][y].g; hist[x][y].b += hue2rgb(color).b; if (hist[x][y].b > maxB) maxB = hist[x][y].b; } } output(hist, w, h, num, maxR, maxG, maxB); }
static void AutoHueState_10Hz( void ) { --mode_autoHueState.delay100ms; if( mode_autoHueState.delay100ms >= (volatile uint8_t)(g_params->hueChangeIntervall) ) { ++mode_autoHueState.curHue; //AutoHueStat_enter(0); uint8_t r; uint8_t g; uint8_t b; mode_autoHueState.curHue %= (HUE_MAX+1); hue2rgb(mode_autoHueState.curHue, &r, &g, &b); SetPWMs(r,g,b); mode_autoHueState.delay100ms = g_params->hueChangeIntervall; } }
void ColorWheel::setColor(const Colour &rgb) { float r = rgb[0], g = rgb[1], b = rgb[2]; float max = std::max({ r, g, b }); float min = std::min({ r, g, b }); float l = (max + min) / 2; if (max == min) { mHue = 0.; mBlack = 1. - l; mWhite = l; } else { float d = max - min, h; /* float s = l > 0.5 ? d / (2 - max - min) : d / (max + min); */ if (max == r) h = (g - b) / d + (g < b ? 6 : 0); else if (max == g) h = (b - r) / d + 2; else h = (r - g) / d + 4; h /= 6; mHue = h; vec3 pt(rgb.r, rgb.g, rgb.b); Colour c = hue2rgb(mHue); vec3 a(c.r, c.g, c.b); vec3 b(0, 0, 0); vec3 w(1, 1, 1); vec3 bary = calcBarycentric(pt, a, w, b); /* Eigen::Matrix<float, 4, 3> M; M.topLeftCorner<3, 1>() = hue2rgb(h).head<3>(); M(3, 0) = 1.; M.col(1) = vec4{ 0., 0., 0., 1. }; M.col(2) = vec4{ 1., 1., 1., 1. }; vec4 rgb4{ rgb[0], rgb[1], rgb[2], 1. }; vec3 bary = M.colPivHouseholderQr().solve(rgb4); */ mBlack = bary[1]; mWhite = bary[2]; } }
void PlayMode::updatePlayerTail() { // add six new player tail particles if(!crashed) { for(int i = 0; i < 6; i++) { float angle = ((float)rng->rand() / (float)RAND_MAX) * 360; float speed = ((float)rng->rand() / (float)RAND_MAX) + 4.5; Uint32 color = hue2rgb(angle - 60); angle = angle / 12 - 195; angle = angle * (3.14159265 / 180); // radian particles->add(140.f, player_pos + 5, (float)cos(angle) * speed, (float)sin(angle) * speed, 30, 3, 0, ParticleSystem::SQUARE, color); } } }
void ColorWheel::setColor(const Color &rgb) { float r = rgb[0], g = rgb[1], b = rgb[2]; float max = std::max({ r, g, b }); float min = std::min({ r, g, b }); float l = (max + min) / 2; if (max == min) { mHue = 0.; mBlack = 1. - l; mWhite = l; } else { float d = max - min, h; /* float s = l > 0.5 ? d / (2 - max - min) : d / (max + min); */ if (max == r) h = (g - b) / d + (g < b ? 6 : 0); else if (max == g) h = (b - r) / d + 2; else h = (r - g) / d + 4; h /= 6; mHue = h; Eigen::Matrix<float, 4, 3> M; auto c = hue2rgb(h).rgb().rgb(); M.topLeftCorner<3, 1>() = Eigen::Map<Eigen::Vector3f>(glm::value_ptr(c)); M(3, 0) = 1.; M.col(1) = Eigen::Vector4f{ 0., 0., 0., 1. }; M.col(2) = Eigen::Vector4f{ 1., 1., 1., 1. }; Eigen::Vector4f rgb4{ rgb[0], rgb[1], rgb[2], 1. }; Eigen::Vector3f bary = M.colPivHouseholderQr().solve(rgb4); mBlack = bary[1]; mWhite = bary[2]; } }
Color ColorWheel::color() const { Color rgb = hue2rgb(mHue); Color black { 0.f, 0.f, 0.f, 1.f }; Color white { 1.f, 1.f, 1.f, 1.f }; return rgb * (1 - mWhite - mBlack) + black * mBlack + white * mWhite; }
bool MySensorsBase::WriteToHardware(const char *pdata, const unsigned char length) { tRBUF *pCmd = (tRBUF *)pdata; if (pCmd->LIGHTING2.packettype == pTypeLighting2) { //Light command int node_id = pCmd->LIGHTING2.id4; int child_sensor_id = pCmd->LIGHTING2.unitcode; if (_tMySensorNode *pNode = FindNode(node_id)) { int light_command = pCmd->LIGHTING2.cmnd; if ((pCmd->LIGHTING2.cmnd == light2_sSetLevel) && (pCmd->LIGHTING2.level == 0)) { light_command = light2_sOff; } else if ((pCmd->LIGHTING2.cmnd == light2_sSetLevel) && (pCmd->LIGHTING2.level == 255)) { light_command = light2_sOn; } if ((light_command == light2_sOn) || (light_command == light2_sOff)) { std::string lState = (light_command == light2_sOn) ? "1" : "0"; if (FindChildWithValueType(node_id, V_LOCK_STATUS) != NULL) { //Door lock SendCommand(node_id, child_sensor_id, MT_Set, V_LOCK_STATUS, lState); } else if ((FindChildWithValueType(node_id, V_SCENE_ON) != NULL) || (FindChildWithValueType(node_id, V_SCENE_OFF) != NULL)) { //Scene Controller SendCommand(node_id, child_sensor_id, MT_Set, (light_command == light2_sOn) ? V_SCENE_ON : V_SCENE_OFF, lState); } else { //normal SendCommand(node_id, child_sensor_id, MT_Set, V_STATUS, lState); } } else if (light_command == light2_sSetLevel) { float fvalue = (100.0f / 15.0f)*float(pCmd->LIGHTING2.level); if (fvalue > 100.0f) fvalue = 100.0f; //99 is fully on int svalue = round(fvalue); std::stringstream sstr; sstr << svalue; SendCommand(node_id, child_sensor_id, MT_Set, V_PERCENTAGE, sstr.str()); } } else { _log.Log(LOG_ERROR, "MySensors: Light command received for unknown node_id: %d", node_id); return false; } } else if (pCmd->LIGHTING2.packettype == pTypeLimitlessLights) { //RGW/RGBW command _tLimitlessLights *pLed = (_tLimitlessLights *)pdata; unsigned char ID1 = (unsigned char)((pLed->id & 0xFF000000) >> 24); unsigned char ID2 = (unsigned char)((pLed->id & 0x00FF0000) >> 16); unsigned char ID3 = (unsigned char)((pLed->id & 0x0000FF00) >> 8); unsigned char ID4 = (unsigned char)pLed->id & 0x000000FF; int node_id = (ID3 << 8) | ID4; int child_sensor_id = pLed->dunit; if (_tMySensorNode *pNode = FindNode(node_id)) { bool bIsRGBW = (pNode->FindChildWithPresentationType(child_sensor_id, S_RGBW_LIGHT) != NULL); if (pLed->command == Limitless_SetRGBColour) { int red, green, blue; float cHue = (360.0f / 255.0f)*float(pLed->value);//hue given was in range of 0-255 int Brightness = 100; int dMax = round((255.0f / 100.0f)*float(Brightness)); hue2rgb(cHue, red, green, blue, dMax); std::stringstream sstr; sstr << std::setw(2) << std::uppercase << std::hex << std::setfill('0') << std::hex << red << std::setw(2) << std::uppercase << std::hex << std::setfill('0') << std::hex << green << std::setw(2) << std::uppercase << std::hex << std::setfill('0') << std::hex << blue; SendCommand(node_id, child_sensor_id, MT_Set, (bIsRGBW == true) ? V_RGBW : V_RGB, sstr.str()); } else if (pLed->command == Limitless_SetColorToWhite) { std::stringstream sstr; int Brightness = 100; int wWhite = round((255.0f / 100.0f)*float(Brightness)); if (!bIsRGBW) { sstr << std::setw(2) << std::uppercase << std::hex << std::setfill('0') << std::hex << wWhite << std::setw(2) << std::uppercase << std::hex << std::setfill('0') << std::hex << wWhite << std::setw(2) << std::uppercase << std::hex << std::setfill('0') << std::hex << wWhite; } else { sstr << "#000000" << std::setw(2) << std::uppercase << std::hex << std::setfill('0') << std::hex << wWhite; } SendCommand(node_id, child_sensor_id, MT_Set, (bIsRGBW == true) ? V_RGBW : V_RGB, sstr.str()); } else if (pLed->command == Limitless_SetBrightnessLevel) { float fvalue = pLed->value; int svalue = round(fvalue); if (svalue > 100) svalue = 100; std::stringstream sstr; sstr << svalue; SendCommand(node_id, child_sensor_id, MT_Set, V_PERCENTAGE, sstr.str()); } else if ((pLed->command == Limitless_LedOff) || (pLed->command == Limitless_LedOn)) { std::string lState = (pLed->command == Limitless_LedOn) ? "1" : "0"; SendCommand(node_id, child_sensor_id, MT_Set, V_STATUS, lState); } } else { _log.Log(LOG_ERROR, "MySensors: Light command received for unknown node_id: %d", node_id); return false; } }
void PlayMode::collisionDetect() { if( (int)player_pos < walls_top[28] && !crashed ) { // crashed into top wall crashExplosion(); crashed = true; } if( (int)player_pos + 10 > walls_bottom[28] && !crashed ) { // crashed into bottom wall crashExplosion(); crashed = true; } if( obstacles[28] && (int)player_pos + 10 > obstacles[28] && (int)player_pos < obstacles[28] + 50 && !crashed ) { // crashed into obstacle crashExplosion(); crashed = true; } // collision of player with stars if( items[28] && (int)player_pos + 10 > items[28] && (int)player_pos < items[28] + 16 && !crashed ) { collected++; score+=1024; items[28] = 0; for(int i = 0; i < 350; i++) { float speed = .5f * (float)rng->rand() / (float)RAND_MAX + .5; float angle = ((float)rng->rand() / (float)RAND_MAX) * 360; particles->add(140, player_pos, (float)cos(angle * (3.14159265 / 180)) * speed - 3, 2 * (float)sin(angle * (3.14159265 / 180)) * speed, rng->rand() % 64, 2, 2, ParticleSystem::PIXEL, hue2rgb(angle + 60)); } floating->add("1024", 130, player_pos, 50); } // detect near-crashes for special power if(!crashed && special < 200) { if((int)player_pos < walls_top[28] + 10) { special++; for(int i = 0; i < 25; i++) { float speed = (float)rng->rand() / (float)RAND_MAX; float angle = ((float)rng->rand() / (float)RAND_MAX) * 360; // TODO radian conversion can be done right here particles->add(145, player_pos + 2, (float)cos(angle * (3.14159265 / 180)) * speed - 3, (float)sin(angle * (3.14159265 / 180)) * speed - .3f, rng->rand() % 100, 6, 4, ParticleSystem::PIXEL, (rng->rand() | 0xFF0000FF) & 0xFFD700FF); } } if((int)player_pos + 10 > walls_bottom[28] - 10) { special++; for(int i = 0; i < 25; i++) { float speed = (float)rng->rand() / (float)RAND_MAX; float angle = ((float)rng->rand() / (float)RAND_MAX) * 360; // TODO radian conversion can be done right here particles->add(145, player_pos + 8, (float)cos(angle * (3.14159265 / 180)) * speed - 3, (float)sin(angle * (3.14159265 / 180)) * speed + .3f, rng->rand() % 100, 6, 4, ParticleSystem::PIXEL, (rng->rand() | 0xFF0000FF) & 0xFFD700FF); } } if(obstacles[28] && (int)player_pos + 10 > obstacles[28] - 10 && (int)player_pos < obstacles[28] + 60) { if(special < 190) special+=10; else special = 200; if(player_pos > obstacles[28]) { for(int i = 0; i < 120; i++) { float speed = 1.5f * (float)rng->rand() / (float)RAND_MAX; float angle = ((float)rng->rand() / (float)RAND_MAX) * 360; // TODO radian conversion can be done right here particles->add(145, player_pos + 2, (float)cos(angle * (3.14159265 / 180)) * speed - 3.5f, (float)sin(angle * (3.14159265 / 180)) * speed - .5f, rng->rand() % 100, 6, 4, ParticleSystem::PIXEL, (rng->rand() | 0xFF0000FF) & 0xFFD700FF); } } else { for(int i = 0; i < 120; i++) { float speed = 1.5f * (float)rng->rand() / (float)RAND_MAX; float angle = ((float)rng->rand() / (float)RAND_MAX) * 360; // TODO radian conversion can be done right here particles->add(145, player_pos + 8, (float)cos(angle * (3.14159265 / 180)) * speed - 3.5f, (float)sin(angle * (3.14159265 / 180)) * speed + .5f, rng->rand() % 100, 6, 4, ParticleSystem::PIXEL, (rng->rand() | 0xFF0000FF) & 0xFFD700FF); } } } } // collision of shots with obstacles for(int i = 0; i < 131; i++) { if(shot[i] && shot[i] + 5 > obstacles[i+28] && shot[i] < obstacles[i+28] + 50) { obstacles[i+28] = 0; if(!justHit) justHit = 3; // If an obstacle was hit by a shot, generate explosion and count score if(justHit == 3) { addExplosion((i+28) * 5, shot[i]); comboTime = 50; score+=512; floating->add("512", (i+28)*5, shot[i], 40); if(hitDuringCombo == 0) { score+=512; floating->add("512", (i+28)*5, shot[i], 40); hitDuringCombo++; } else { score+=512*(pow(2, hitDuringCombo)); std::ostringstream s; s << "512 x" << pow(2, hitDuringCombo); floating->add(s.str().c_str(), (i+28)*5, shot[i], 40); hitDuringCombo++; } } if(justHit) // too warm here to think straight - there must be a more elegant way to do this justHit--; } } if(comboTime) comboTime--; else hitDuringCombo = 0; // ...and with walls for(int i = 1; i < 130; i++) // only from 1..130 because we have to delete 3 elements in the shots array to make sure the shot is erased completely if(shot[i] && (shot[i] + 5 > walls_bottom[i+28] || shot[i] < walls_top[i+28])) { shot[i-1] = shot[i] = shot[i+1] = 0; } }
static void fill_rgb( color_type* rgb, NextWorld::WorldUnit* unit, NextWorld::WorldBase* world, uint32 len) { const uint32 max_w = world->mWidth; uint32 w = 0; UnitDrawConfig drawConfig; color_type* p_rgb; float tmp; float tmp2; float h; float s; float l; memset(rgb, 0, len * 3); while(w < max_w) { #if 0 if(unit[w].z > world->mZBase) { unit[w].natureAttr.type = NextWorld::NatureAttr::TYPE_EARTH; } else { unit[w].natureAttr.type = NextWorld::NatureAttr::TYPE_WATER; } #endif p_rgb = &rgb[w * 2]; world->setupDraw( &unit[w], &drawConfig ); // HSL to RGB if( 0 == drawConfig.hsl_color.s ) { p_rgb->r = drawConfig.hsl_color.l * 255.0f; p_rgb->g = p_rgb->r; p_rgb->b = p_rgb->r; } else { h = drawConfig.hsl_color.h / 360.0f; s = drawConfig.hsl_color.s / 100.0f; l = drawConfig.hsl_color.l / 100.0f; if( l < 0.5f ) { tmp = l * ( 1.0f + s ); } else { tmp = ( l + s ) - l * s; } tmp2 = 2.0f * l - tmp; //printf(">%f %f %f \n", h, s, l); //printf("<%f %f %f \n", tmp, tmp2, h); p_rgb->r = (uint8)hue2rgb( tmp, tmp2, h + 0.3333f ); // 1/3 = 0.3333 p_rgb->g = (uint8)hue2rgb( tmp, tmp2, h ); p_rgb->b = (uint8)hue2rgb( tmp, tmp2, h - 0.3333f ); //printf("<<%d %d %d \n", p_rgb->r, p_rgb->g, p_rgb->b ); } // Copy memcpy( &rgb[w * 2 + 1], &rgb[w * 2], sizeof(color_type) ); w++; } }
bool BleBox::WriteToHardware(const char *pdata, const unsigned char length) { const tRBUF *output = reinterpret_cast<const tRBUF*>(pdata); if (output->ICMND.packettype == pTypeLighting2 && output->LIGHTING2.subtype == sTypeAC) { std::string IPAddress = GetDeviceIP(output); switch (output->LIGHTING2.unitcode) { case 0: { std::string state; if (output->LIGHTING2.cmnd == light2_sOn) { state = "1"; } else { state = "0"; } Json::Value root = SendCommand(IPAddress, "/s/" + state); if (root == "") return false; if (root["state"].empty() == true) { _log.Log(LOG_ERROR, "BleBox: node 'state' missing!"); return false; } if (root["state"].asString() != state) { _log.Log(LOG_ERROR, "BleBox: state not changed!"); return false; } break; } case 1: { std::string state; if (output->LIGHTING2.cmnd == light2_sOn) { state = "u"; } else if (output->LIGHTING2.cmnd == light2_sOff) { state = "d"; } else { int percentage = output->LIGHTING2.level * 100 / 15; state = boost::to_string(percentage); } Json::Value root = SendCommand(IPAddress, "/s/" + state); if (root == "") return false; if (root["state"].empty() == true) { _log.Log(LOG_ERROR, "BleBox: node 'state' missing!"); return false; } //if (root["state"].asString() != state) //{ // _log.Log(LOG_ERROR, "BleBox: state not changed!"); // return false; //} break; } case 2: { std::string level; if (output->LIGHTING2.cmnd == light2_sOn) { level = "ff"; } else if (output->LIGHTING2.cmnd == light2_sOff) { level = "00"; } else { int percentage = output->LIGHTING2.level * 255 / 15; char value[4]; sprintf(value, "%x", percentage); level = value; } Json::Value root = SendCommand(IPAddress, "/s/" + level); if (root == "") return false; if (root["light"].empty() == true) { _log.Log(LOG_ERROR, "BleBox: node 'light' missing!"); return false; } if (root["light"]["currentColor"].empty() == true) { _log.Log(LOG_ERROR, "BleBox: node 'currentColor' missing!"); return false; } if (root["light"]["currentColor"].asString() != level) // TODO or desiredcolor ?? { _log.Log(LOG_ERROR, "BleBox: light not changed!"); return false; } break; } } } if (output->ICMND.packettype == pTypeLimitlessLights && output->LIGHTING2.subtype == sTypeLimitlessRGBW) { std::string IPAddress = GetDeviceRevertIP(output); const _tLimitlessLights *pLed = reinterpret_cast<const _tLimitlessLights *>(pdata); int red, green, blue; float cHue = (360.0f / 255.0f)*float(pLed->value);//hue given was in range of 0-255 hue2rgb(cHue, red, green, blue); char level[10]; sprintf(level, "%02x%02x%02x%02x", red, green, blue, 255); std::string state(level); Json::Value root = SendCommand(IPAddress, "/s/" + state); if (root == "") return false; if (root["rgbw"].empty() == true) { _log.Log(LOG_ERROR, "BleBox: node 'rgbw' missing!"); return false; } if (root["rgbw"]["desiredColor"].empty() == true) { _log.Log(LOG_ERROR, "BleBox: node 'desiredColor' missing!"); return false; } if (root["rgbw"]["desiredColor"].asString() != state) { _log.Log(LOG_ERROR, "BleBox: rgbw not changed!"); return false; } } return true; }