{"id":4829,"date":"2018-10-19T22:47:08","date_gmt":"2018-10-19T14:47:08","guid":{"rendered":"https:\/\/unitycoder.com\/blog\/?p=4829"},"modified":"2018-10-19T22:49:00","modified_gmt":"2018-10-19T14:49:00","slug":"hsv-debugging-shader","status":"publish","type":"post","link":"https:\/\/unitycoder.com\/blog\/2018\/10\/19\/hsv-debugging-shader\/","title":{"rendered":"HSV Debugging and Color Match Shader"},"content":{"rendered":"<p>Quick test for color matching using shader,<br \/>\nby converting colors into HSV and comparing target color with h-s-v threshold values.<\/p>\n<p>See Shader source at the bottom.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4830\" data-permalink=\"https:\/\/unitycoder.com\/blog\/2018\/10\/19\/hsv-debugging-shader\/color-match-shader-preview\/\" data-orig-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2018\/10\/color-match-shader-preview.gif?fit=454%2C267&amp;ssl=1\" data-orig-size=\"454,267\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"color-match-shader-preview\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2018\/10\/color-match-shader-preview.gif?fit=454%2C267&amp;ssl=1\" class=\"alignnone size-full wp-image-4830\" src=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2018\/10\/color-match-shader-preview.gif?resize=454%2C267&#038;ssl=1\" alt=\"\" width=\"454\" height=\"267\" \/><br \/>\n(main image: Changing target color, shows inverted color for the texture in that matching hue position)<\/p>\n<hr \/>\n<p>Shader has different draw modes for debugging:<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4831\" data-permalink=\"https:\/\/unitycoder.com\/blog\/2018\/10\/19\/hsv-debugging-shader\/color-match-shader\/\" data-orig-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2018\/10\/color-match-shader.jpg?fit=469%2C300&amp;ssl=1\" data-orig-size=\"469,300\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"color-match-shader\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2018\/10\/color-match-shader.jpg?fit=469%2C300&amp;ssl=1\" class=\"alignnone size-full wp-image-4831\" src=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2018\/10\/color-match-shader.jpg?resize=469%2C300&#038;ssl=1\" alt=\"\" width=\"469\" height=\"300\" srcset=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2018\/10\/color-match-shader.jpg?w=469&amp;ssl=1 469w, https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2018\/10\/color-match-shader.jpg?resize=300%2C192&amp;ssl=1 300w\" sizes=\"auto, (max-width: 469px) 100vw, 469px\" \/><\/p>\n<p>for RemapHue, need to supply this texture in &#8220;HueGradientTexture&#8221; field<br \/>\n<img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4832\" data-permalink=\"https:\/\/unitycoder.com\/blog\/2018\/10\/19\/hsv-debugging-shader\/hsv-colors\/\" data-orig-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2018\/10\/hsv-colors.png?fit=256%2C25&amp;ssl=1\" data-orig-size=\"256,25\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"hsv-colors\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2018\/10\/hsv-colors.png?fit=256%2C25&amp;ssl=1\" class=\"alignnone size-full wp-image-4832\" src=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2018\/10\/hsv-colors.png?resize=256%2C25&#038;ssl=1\" alt=\"\" width=\"256\" height=\"25\" \/><\/p>\n<p>Here is my testing texture<br \/>\n<img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4833\" data-permalink=\"https:\/\/unitycoder.com\/blog\/2018\/10\/19\/hsv-debugging-shader\/rainbow\/\" data-orig-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2018\/10\/rainbow.png?fit=512%2C512&amp;ssl=1\" data-orig-size=\"512,512\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"rainbow\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2018\/10\/rainbow.png?fit=512%2C512&amp;ssl=1\" class=\"alignnone size-full wp-image-4833\" src=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2018\/10\/rainbow.png?resize=512%2C512&#038;ssl=1\" alt=\"\" width=\"512\" height=\"512\" srcset=\"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2018\/10\/rainbow.png?w=512&amp;ssl=1 512w, https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2018\/10\/rainbow.png?resize=150%2C150&amp;ssl=1 150w, https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2018\/10\/rainbow.png?resize=300%2C300&amp;ssl=1 300w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/p>\n<hr \/>\n<p><strong>One question remains:<\/strong><br \/>\nWhy do i need to invert targetHue for the hueDistance calculation? (otherwise it picks wrong hue..)<br \/>\n<span style=\"color: #000080;\"><em>float hueDist = abs(sourceHSV.x &#8211; (1-targetHSV.x));<\/em><\/span><\/p>\n<hr \/>\n<p><strong>References:<\/strong><br \/>\n&#8211; RGB to HSV <a href=\"http:\/\/lolengine.net\/blog\/2013\/07\/27\/rgb-to-hsv-in-glsl\" target=\"_blank\" rel=\"noopener\">http:\/\/lolengine.net\/blog\/2013\/07\/27\/rgb-to-hsv-in-glsl<\/a><\/p>\n<hr \/>\n<p><strong>Shader source:<\/strong><br \/>\n&#8211; <a href=\"https:\/\/github.com\/UnityCommunity\/UnityLibrary\/blob\/master\/Assets\/Shaders\/2D\/Debug\/HSVDebugger.shader\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/UnityCommunity\/UnityLibrary\/blob\/master\/Assets\/Shaders\/2D\/Debug\/HSVDebugger.shader<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Quick test for color matching using shader, by converting colors into HSV and comparing target color with h-s-v threshold values. See Shader source at the bottom. (main image: Changing target color, shows inverted color for the texture in that matching hue position) Shader has different [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4830,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[13,3],"tags":[79,389,738,1060,1061,575,14,1062],"class_list":["post-4829","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-shaders","category-unity3d","tag-color","tag-convert","tag-debug","tag-hsv","tag-match","tag-rgb","tag-shader","tag-similar"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/unitycoder.com\/blog\/wp-content\/uploads\/2018\/10\/color-match-shader-preview.gif?fit=454%2C267&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/p1KTaT-1fT","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/4829","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/comments?post=4829"}],"version-history":[{"count":3,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/4829\/revisions"}],"predecessor-version":[{"id":4836,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/posts\/4829\/revisions\/4836"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/media\/4830"}],"wp:attachment":[{"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/media?parent=4829"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/categories?post=4829"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unitycoder.com\/blog\/wp-json\/wp\/v2\/tags?post=4829"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}