Beispiel #1
0
    public static HueHSBColor FromColor(Color color)
    {
        HueHSBColor ret = new HueHSBColor(0f, 0f, 0f, color.a);

          float r = color.r;
          float g = color.g;
          float b = color.b;

          float max = Mathf.Max(r, Mathf.Max(g, b));

          if (max <= 0)
          {
         return ret;
          }

          float min = Mathf.Min(r, Mathf.Min(g, b));
          float dif = max - min;

          if (max > min)
          {
         if (g == max)
         {
            ret.h = (b - r) / dif * 60f + 120f;
         }
         else if (b == max)
         {
            ret.h = (r - g) / dif * 60f + 240f;
         }
         else if (b > g)
         {
            ret.h = (g - b) / dif * 60f + 360f;
         }
         else
         {
            ret.h = (g - b) / dif * 60f;
         }
         if (ret.h < 0)
         {
            ret.h = ret.h + 360f;
         }
          }
          else
          {
         ret.h = 0;
          }

          ret.h *= 1f / 360f;
          ret.s = (dif / max) * 1f;
          ret.b = max;

          return ret;
    }
Beispiel #2
0
    HTTP.Request UpdateLight(Light l)
    {
        Color prevColor = l.LastColor();
          float prevFade = l.LastFade();
          if (!l.ShouldPush())
         return null;

          string apiCall = "/api/" + User + "/lights/" + l.id + "/state";
          float fade = Mathf.Clamp01(l.fade);
          HueHSBColor hsbColor = new HueHSBColor(l.color);
          int transitionTime = (int)(l.transitionTime * 10.0f); //this is specified in hundreds of millisecs (i.e 10 = 1000 ms = 1s)
          bool on = (l.on && (fade > 0.0f));
          string body = "{\"on\": " + (on ? "true" : "false") +
                    " \"hue\": " + (int)(hsbColor.h * 65535.0f) +
                    " \"sat\": " + (int)(hsbColor.s * 255.0f) +
                    " \"bri\": " + (int)(hsbColor.b * fade * 255.0f) +
                    " \"transitiontime\": " + transitionTime +
                     //" \"effect\":\"colorloop\"" +
                    "}";
         /* on = true;
          string body = "{\"on\": " + (on ? "true" : "false") +
                    " \"effect\":\"colorloop\"" +
                    "}";*/
          string url = "http://" + BridgeIP + apiCall;
          //Debug.Log("URL: " + url + " body: " + body + " at Time: " + Time.time + " deltaSinceLastUpdate: " + l.TimeSinceLastUpdate() + "\n");
          HTTP.Request request = new HTTP.Request("put", "http://" + BridgeIP + apiCall, JSON.JsonDecode(body) as Hashtable);
          request.Send();

          l.Pushed();

          //notify anyone who cares
          SendEvents(l, prevColor, prevFade);

          return request;
    }
Beispiel #3
0
    /*void LateUpdate()
       {
      for (int i = 0; i < Lights.Length; i++)
      {
         HueMessenger.Light l = Lights[i];
         l.
      }
       }*/
    public IEnumerator UpdateLights()
    {
        _isRequesting = true;

          foreach (Light l in Lights)
          {
         Color prevColor = l.LastColor();
         float prevFade = l.LastFade();

         if (!l.ShouldPush())
            continue;

         string apiCall = "/api/" + User + "/lights/" + l.id + "/state";
         float fade = Mathf.Clamp01(l.fade);
         HueHSBColor hsbColor = new HueHSBColor(l.color);
         int transitionTime = (int)(l.transitionTime * 10.0f); //this is specified in hundreds of millisecs (i.e 10 = 1000 ms = 1s)
         string body = "{\"on\": " + ((l.on && (fade > 0.0f)) ? "true" : "false") +
                       " \"hue\": " + (int)(hsbColor.h * 65535.0f) +
                       " \"sat\": " + (int)(hsbColor.s * 255.0f) +
                       " \"bri\": " + (int)(hsbColor.b * fade * 255.0f) +
                       " \"transitiontime\": " + transitionTime +
                       //" \"effect\":\"colorloop\"" +
                       "}";
         string url = "http://" + BridgeIP + apiCall;
         //Debug.Log("URL: " + url + " body: " + body + " at Time: " + Time.time + " deltaSinceLastUpdate: " + l.TimeSinceLastUpdate() + "\n");
         HTTP.Request request = new HTTP.Request("put", "http://" + BridgeIP + apiCall, JSON.JsonDecode(body) as Hashtable);
         request.Send();

         l.Pushed();

         SendEvents(l, prevColor, prevFade);

         if (!FireAndForget)
         {
            float startTime = Time.time;
            while (!request.isDone)
            {
               yield return null;
            }
            Debug.Log("Received response in " + (Time.time - startTime) +  " secs!");

            if (!request.response.Text.Contains("success"))
               Debug.Log("Error updating light: " + request.response.Text);
         }
          }

          _isRequesting = false;
    }
Beispiel #4
0
    public static HueHSBColor Lerp(HueHSBColor a, HueHSBColor b, float t)
    {
        float h, s;

          //check special case black (color.b==0): interpolate neither hue nor saturation!
          //check special case grey (color.s==0): don't interpolate hue!
          if (a.b == 0)
          {
         h = b.h;
         s = b.s;
          }
          else if (b.b == 0)
          {
         h = a.h;
         s = a.s;
          }
          else
          {
         if (a.s == 0)
         {
            h = b.h;
         }
         else if (b.s == 0)
         {
            h = a.h;
         }
         else
         {
            // works around bug with LerpAngle
            float angle = Mathf.LerpAngle(a.h * 360f, b.h * 360f, t);
            while (angle < 0f)
               angle += 360f;
            while (angle > 360f)
               angle -= 360f;
            h = angle / 360f;
         }
         s = Mathf.Lerp(a.s, b.s, t);
          }
          return new HueHSBColor(h, s, Mathf.Lerp(a.b, b.b, t), Mathf.Lerp(a.a, b.a, t));
    }
Beispiel #5
0
    public static Color ToColor(HueHSBColor hsbColor)
    {
        float r = hsbColor.b;
          float g = hsbColor.b;
          float b = hsbColor.b;
          if (hsbColor.s != 0)
          {
         float max = hsbColor.b;
         float dif = hsbColor.b * hsbColor.s;
         float min = hsbColor.b - dif;

         float h = hsbColor.h * 360f;

         if (h < 60f)
         {
            r = max;
            g = h * dif / 60f + min;
            b = min;
         }
         else if (h < 120f)
         {
            r = -(h - 120f) * dif / 60f + min;
            g = max;
            b = min;
         }
         else if (h < 180f)
         {
            r = min;
            g = max;
            b = (h - 120f) * dif / 60f + min;
         }
         else if (h < 240f)
         {
            r = min;
            g = -(h - 240f) * dif / 60f + min;
            b = max;
         }
         else if (h < 300f)
         {
            r = (h - 240f) * dif / 60f + min;
            g = min;
            b = max;
         }
         else if (h <= 360f)
         {
            r = max;
            g = min;
            b = -(h - 360f) * dif / 60 + min;
         }
         else
         {
            r = 0;
            g = 0;
            b = 0;
         }
          }

          return new Color(Mathf.Clamp01(r), Mathf.Clamp01(g), Mathf.Clamp01(b), hsbColor.a);
    }
Beispiel #6
0
    public static void Test()
    {
        HueHSBColor color;

          color = new HueHSBColor(Color.red);
          Debug.Log("red: " + color);

          color = new HueHSBColor(Color.green);
          Debug.Log("green: " + color);

          color = new HueHSBColor(Color.blue);
          Debug.Log("blue: " + color);

          color = new HueHSBColor(Color.grey);
          Debug.Log("grey: " + color);

          color = new HueHSBColor(Color.white);
          Debug.Log("white: " + color);

          color = new HueHSBColor(new Color(0.4f, 1f, 0.84f, 1f));
          Debug.Log("0.4, 1f, 0.84: " + color);

          Debug.Log("164,82,84   .... 0.643137f, 0.321568f, 0.329411f  :" + ToColor(new HueHSBColor(new Color(0.643137f, 0.321568f, 0.329411f))));
    }