{"id":207535,"date":"2025-07-08T11:54:35","date_gmt":"2025-07-08T03:54:35","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207535\/"},"modified":"2025-07-08T11:54:35","modified_gmt":"2025-07-08T03:54:35","slug":"golang%e4%bd%bfrgba%e5%9b%be%e5%83%8f%e6%98%be%e7%a4%ba%e5%a5%87%e6%80%aa%e7%9a%84%e9%a2%9c%e8%89%b2","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207535\/","title":{"rendered":"Golang\u4f7fRGBA\u56fe\u50cf\u663e\u793a\u5947\u602a\u7684\u989c\u8272"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>Golang\u4f7fRGBA\u56fe\u50cf\u663e\u793a\u5947\u602a\u7684\u989c\u8272<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-23 13:36:35<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGolang\u4f7fRGBA\u56fe\u50cf\u663e\u793a\u5947\u602a\u7684\u989c\u8272\u300b<\/span>\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6240\u4ee5\u6211\u6309\u7167\u67d0\u79cd\u6a21\u5f0f\u9010\u50cf\u7d20\u5236\u4f5c\u4e00\u4e9b rgba \u56fe\u50cf\uff0c\u7136\u540e\u5c06\u5b83\u4eec\u4fdd\u5b58\u4e3a png\uff0c\u5e76\u6ce8\u610f\u5230\u5f53 alpha \u901a\u9053\u4f7f\u7528\u67d0\u4e9b\u989c\u8272\u66f4\u6539\u65f6\uff0c\u5f53\u5b58\u50a8\u4e3a png \u65f6\uff0c\u5b83\u4f1a\u66f4\u6539\u6574\u4e2a\u50cf\u7d20\u989c\u8272\u3002 <\/p>\n<p>\u6211\u505a\u4e86\u4e00\u4e2a\u6d4b\u8bd5\u6765\u663e\u793a\u5f53\u524d\u53d1\u751f\u7684\u60c5\u51b5\uff1a<\/p>\n<pre>img := image.NewRGBA(image.Rect(0, 0, 250, 250))\n    for y := 0; y &lt; height; y++ {\n        for x := 0; x &lt; width; x++ {\n            f.Read(b)\n            img.SetRGBA(x, y, color.RGBA{\n                249,\n                214,\n                133,\n                255,\n            })\n        }\n    }\n    var buff bytes.Buffer\n    err := png.Encode(&amp;buff, img)\n    if err != nil {\n        log.Println(err)\n        return\n    }<\/pre>\n<p>\u8fd9\u5c06\u6253\u5370\u5f69\u8272\u56fe\u50cf<em>#f9d685<\/em>\u3002\u4f46\u662f\uff0c\u5982\u679c\u6211\u5c06 alpha \u66f4\u6539\u4e3a 200\uff0c\u5b83\u5c06\u6253\u5370\u53e6\u4e00\u4e2a\u5177\u6709 <em>#6844bc<\/em> \u548c\u900f\u660e\u5ea6\u7684\u989c\u8272\uff0c\u800c\u4e0d\u662f\u4f7f\u7528\u5176\u900f\u660e\u5ea6\u6253\u5370\u539f\u59cb\u989c\u8272\u3002 <\/p>\n<p>\u6709\u529e\u6cd5\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u5417\uff1f\u6211\u76f8\u4fe1\u8fd9\u662f\u56e0\u4e3a\u6211\u9519\u8fc7\u4e86\u4e00\u4e9b\u4e1c\u897f\uff0c\u4f46\u65e0\u6cd5\u771f\u6b63\u5f04\u6e05\u695a\uff0c\u4e5f\u6ca1\u6709\u627e\u5230\u4efb\u4f55\u4e0e\u6211\u5728 google\/here \u4e0a\u53d1\u751f\u7684\u4e8b\u60c5\u7c7b\u4f3c\u7684\u4e1c\u897f\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u8fd9\u4e2a\u5f88\u7b80\u5355\uff1a<\/p>\n<pre>go doc color.rgba<\/pre>\n<p>\u5c01\u88c5\u989c\u8272 \/\/ \u5bfc\u5165\u201cimage\/color\u201d<\/p>\n<pre>type RGBA struct {\n    R, G, B, A uint8\n}<\/pre>\n<p>rgba \u4ee3\u8868\u4f20\u7edf\u7684 32 \u4f4d alpha \u9884\u4e58\u989c\u8272\uff0c\u6709 8 \u7ea2\u8272\u3001\u7eff\u8272\u3001\u84dd\u8272\u548c alpha \u7684\u6bcf\u4e2a\u4f4d\u3002<\/p>\n<p>alpha \u9884\u4e58\u989c\u8272\u5206\u91cf c \u5df2\u6309 alpha (a) \u7f29\u653e\uff0c\u56e0\u6b64 \u5177\u6709\u6709\u6548\u503c 0 &lt;= c &lt;= a\u3002<\/p>\n<p>\u60a8\u53ef\u80fd\u6b63\u5728\u5bfb\u627e <code>color.nrgba<\/code>\u3002<\/p>\n<p>\uff08\u603b\u662f\uff0c\u771f\u7684\u603b\u662f\uff0c\u67e5\u9605\u6240\u6d89\u53ca\u7c7b\u578b\u548c\u51fd\u6570\u7684\u6587\u6863\u3002\u603b\u662f\u3002\uff09<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aGolang\u4f7fRGBA\u56fe\u50cf\u663e\u793a\u5947\u602a\u7684\u989c\u8272\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207535","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207535","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207535"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207535\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}