{"id":530,"date":"2017-12-04T14:25:58","date_gmt":"2017-12-04T06:25:58","guid":{"rendered":"https:\/\/www.dd-home.top\/?p=530"},"modified":"2017-12-04T14:25:58","modified_gmt":"2017-12-04T06:25:58","slug":"rgb%e5%92%8chsv-%e4%b9%8b%e9%97%b4%e7%9a%84%e8%bd%ac%e6%8d%a2","status":"publish","type":"post","link":"https:\/\/www.dd-home.top\/?p=530","title":{"rendered":"RGB\u548cHSV \u4e4b\u95f4\u7684\u8f6c\u6362"},"content":{"rendered":"<p><!--more--><\/p>\n<pre>public static float[] rgb2hsb(int rgbR, int rgbG, int rgbB) {\n    assert 0 &lt;= rgbR &amp;&amp; rgbR &lt;= 255;\n    assert 0 &lt;= rgbG &amp;&amp; rgbG &lt;= 255;\n    assert 0 &lt;= rgbB &amp;&amp; rgbB &lt;= 255;\n    int[] rgb = new int[] { rgbR, rgbG, rgbB };\n    Arrays.sort(rgb);\n    int max = rgb[2];\n    int min = rgb[0];\n\n    float hsbB = max \/ 255.0f;\n    float hsbS = max == 0 ? 0 : (max - min) \/ (float) max;\n\n    float hsbH = 0;\n    if (max == rgbR &amp;&amp; rgbG &gt;= rgbB) {\n        hsbH = (rgbG - rgbB) * 60f \/ (max - min) + 0;\n    } else if (max == rgbR &amp;&amp; rgbG &lt; rgbB) {\n        hsbH = (rgbG - rgbB) * 60f \/ (max - min) + 360;\n    } else if (max == rgbG) {\n        hsbH = (rgbB - rgbR) * 60f \/ (max - min) + 120;\n    } else if (max == rgbB) {\n        hsbH = (rgbR - rgbG) * 60f \/ (max - min) + 240;\n    }\n\n    return new float[] { hsbH, hsbS, hsbB };\n}\n\npublic static int[] hsb2rgb(float h, float s, float v) {\n    assert Float.compare(h, 0.0f) &gt;= 0 &amp;&amp; Float.compare(h, 360.0f) &lt;= 0;\n    assert Float.compare(s, 0.0f) &gt;= 0 &amp;&amp; Float.compare(s, 1.0f) &lt;= 0;\n    assert Float.compare(v, 0.0f) &gt;= 0 &amp;&amp; Float.compare(v, 1.0f) &lt;= 0;\n\n    float r = 0, g = 0, b = 0;\n    int i = (int) ((h \/ 60) % 6);\n    float f = (h \/ 60) - i;\n    float p = v * (1 - s);\n    float q = v * (1 - f * s);\n    float t = v * (1 - (1 - f) * s);\n    switch (i) {\n        case 0:\n            r = v;\n            g = t;\n            b = p;\n            break;\n        case 1:\n            r = q;\n            g = v;\n            b = p;\n            break;\n        case 2:\n            r = p;\n            g = v;\n            b = t;\n            break;\n        case 3:\n            r = p;\n            g = q;\n            b = v;\n            break;\n        case 4:\n            r = t;\n            g = p;\n            b = v;\n            break;\n        case 5:\n            r = v;\n            g = p;\n            b = q;\n            break;\n        default:\n            break;\n    }\n    return new int[] { (int) (r * 255.0), (int) (g * 255.0),\n            (int) (b * 255.0) };\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-530","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.dd-home.top\/index.php?rest_route=\/wp\/v2\/posts\/530","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dd-home.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dd-home.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dd-home.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dd-home.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=530"}],"version-history":[{"count":0,"href":"https:\/\/www.dd-home.top\/index.php?rest_route=\/wp\/v2\/posts\/530\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dd-home.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=530"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dd-home.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=530"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dd-home.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=530"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}