js顏色調試器

来源:https://www.cnblogs.com/weihexinCode/archive/2022/05/28/16320422.html
-Advertisement-
Play Games

1 /* ColorTestViewer 顏色調試器 2 3 attribute: 4 onchange: Function; //顏色改變回調; 預設null 5 6 //以下屬性不建議直接修改 7 rgb: RGBColor; //rgb模式顏色 8 hsv: Object{h,s,v}; // ...


  1 /* ColorTestViewer 顏色調試器
  2 
  3 attribute:
  4     onchange: Function; //顏色改變回調; 預設null
  5 
  6     //以下屬性不建議直接修改
  7     rgb: RGBColor;         //rgb模式顏色
  8     hsv: Object{h,s,v};    //hsv模式顏色
  9     alpha: Number;        //透明度
 10 
 11 method:
 12     update(): undefined;        //更新控制項視圖 (通常控制項被喚出時調用此方法, 前提是在喚出前控制項顏色發生了改變)
 13     setValue(r, g, b, a): this; //設置控制項的顏色
 14 
 15 demo:
 16     const ctv = new ColorTestViewer({width: 200})
 17     .setValue(0, 0, 255, 1).update(false)
 18     .pos(100, 100).render();
 19 
 20     ctv.onchange = v => console.log(v);
 21 
 22 */
 23 class ColorTestViewer extends CanvasAnimateRender{
 24 
 25     get value(){
 26         return this.rgb.getRGBA(Math.floor(this.alpha * 1000) / 1000);
 27     }
 28 
 29     constructor(option = {}){
 30         super(option);
 31 
 32         this.rgb = new RGBColor(255);
 33         this.hsv = this.rgb.getHSV();
 34         this.alpha = 1;
 35         this.cae = null;
 36         this.onchange = null;
 37 
 38         this.viewSVColor = null;
 39         this.viewSVsv = null;
 40         this.viewSVCursor = null;
 41 
 42         this.viewHValueBG = null;
 43         this.viewHValue = null;
 44         this.viewHScroll = null;
 45         this.viewHCursor = null;
 46 
 47         this.viewAScroll = null;
 48         this.viewACursor = null;
 49 
 50         this.viewColorInfo = null;
 51 
 52         //預設樣式
 53         if(option.canvas === undefined){
 54             const width = option.width || 200, height = option.height || (width * 0.8), margin = 2, 
 55             h5 = height * 0.5, h1 = height * 0.1, h3 = height * 0.3;
 56 
 57             this.size(width + margin * 2, height + margin * 5);
 58 
 59             this.initViewSVColor(width, h5);
 60             this.initViewHScroll(width, h1);
 61             this.initViewAScroll(width, h1);
 62             this.initViewHValue(h3, h3);
 63             this.initViewColorInfo(width - h3, h3);
 64 
 65             this.setViewSVPos(margin, margin);
 66             this.setViewHScrollPos(margin, h5 + margin * 2);
 67             this.setViewAScrollPos(margin, h5 + h1 + margin * 3);
 68             this.setViewHValuePos(margin, h5 + h1 * 2 + margin * 4);
 69             this.viewColorInfo.pos(this.viewHValue.box.maxX(), this.viewHValue.box.y);
 70             
 71             this.initList();
 72             this.initEvent();
 73 
 74         }
 75         
 76     }
 77 
 78     update(u){
 79         if(this.viewSVColor !== null){
 80             this.updateViewSVCursor();
 81             this.updateViewSVColor();
 82             this.updateViewHValue();
 83             this.updateViewHCursor();
 84             this.updateViewACursor();
 85             this.updateViewColorInfo();
 86             if(u === true) this.redraw();
 87         }
 88 
 89         return this;
 90     }
 91 
 92     setValue(r, g, b, a){
 93         this.rgb.r = r;
 94         this.rgb.g = g;
 95         this.rgb.b = b;
 96         this.alpha = a;
 97         this.rgb.getHSV(this.hsv);
 98         
 99         return this;
100     }
101 
102     setValueString(color){
103         if(typeof color !== "string") return this;
104         var _color = this.getColor(color);
105         
106         if(_color[0] === "#"){
107             const len = _color.length;
108             if(len === 4){
109                 _color = _color.slice(1);
110                 this.rgb.setFormHex(parseInt("0x"+_color + "" + _color));
111             }
112             else if(len === 7) this.rgb.setFormHex(parseInt("0x"+_color.slice(1)));
113             this.alpha = 1;
114             this.rgb.getHSV(this.hsv);
115             
116         }
117 
118         else if(_color[0] === "r" && _color[1] === "g" && _color[2] === "b"){
119             const arr = [];
120             for(let k = 0, len = _color.length, v = "", is = false; k < len; k++){
121                 
122                 if(is === true){
123                     if(_color[k] === "," || _color[k] === ")"){
124                         arr.push(parseFloat(v));
125                         v = "";
126                     }
127                     else v += _color[k];
128                     
129                 }
130 
131                 else if(_color[k] === "(") is = true;
132                 
133             }
134 
135             this.setValue(arr[0], arr[1], arr[2], arr[3] === undefined ? 1 : arr[3]);
136             
137         }
138         
139         return this;
140     }
141 
142     getColor(str){ //檢測 str 是否是顏色類型(16進位, rgb, rgba, 英文); 如果是則返回去除掉空格後的字元串顏色(英文則返回對應16進位顏色)
143         var _color = "";
144         for(let k = 0, len = str.length; k < len; k++){
145             if(str[k] === " ") continue;
146             _color += str[k];
147         }
148         
149         if(_color[0] === "#" || (_color[0] === "r" && _color[1] === "g" && _color[2] === "b")) return _color;
150         else{
151             for(let k = 0, len = ColorRefTable.length; k < len; k++){
152                 str = ColorRefTable[k];
153                 if(str[0] === _color) return str[1];
154             }
155         }
156 
157         return "";
158     }
159 
160     exit(){
161         if(this.cae){
162             this.onUpSV();
163             this.onUpH();
164             this.onUpA();
165         }
166 
167         if(this.domElement.parentElement) this.domElement.parentElement.removeChild(this.domElement);
168 
169     }
170 
171 
172     //event
173     initEvent(){
174         
175         const cae = new CanvasAnimateEvent(this);
176 
177         //SV
178         const setSV = (pageX, pageY) => {
179             pageX = (pageX - this.domElementRect.x - this.viewSVColor.box.x) / this.viewSVColor.box.w * 100;
180             pageY = (1 - (pageY - this.domElementRect.y - this.viewSVColor.box.y) / this.viewSVColor.box.h) * 100;
181             if(pageX < 0) pageX = 0;
182             else if(pageX > 100) pageX = 100;
183             if(pageY < 0) pageY = 0;
184             else if(pageY > 100) pageY = 100;
185             if(this.hsv.s !== pageX || this.hsv.v !== pageY){
186                 this.hsv.s = pageX;
187                 this.hsv.v = pageY;
188                 this.rgb.setFormHSV(this.hsv.h, this.hsv.s, this.hsv.v);
189                 if(typeof this.onchange === "function") this.onchange(this.value);
190                 this.updateViewHValue();
191                 this.updateViewColorInfo();
192                 this.updateViewSVCursor();
193                 this.redraw();
194             }
195 
196         },
197 
198         onMoveSV = event => {
199             setSV(event.pageX, event.pageY);
200         },
201 
202         onUpSV = () => {
203             document.body.removeEventListener("pointerup", onUpSV);
204             document.body.removeEventListener("pointermove", onMoveSV);
205             cae.remove(this.viewSVCursor, 'up', onUpSV);
206             cae.remove(this.viewSVCursor, 'move', onMoveSV);
207             
208         },
209 
210         onDownSV = event => {
211             setSV(event.pageX, event.pageY);
212             cae.add(this.viewSVCursor, "up", onUpSV);
213             cae.add(this.viewSVCursor, "move", onMoveSV);
214             document.body.addEventListener("pointerup", onUpSV);
215             document.body.addEventListener("pointermove", onMoveSV);
216             
217         }
218 
219         cae.add(this.viewSVColor, "down", onDownSV);
220         cae.add(this.viewSVCursor, "down", onDownSV);
221         this.onUpSV = onUpSV;
222 
223 
224         //H
225         const setH = (pageX) => {
226             pageX = (pageX - this.domElementRect.x - this.viewHScroll.box.x) / this.viewHScroll.box.w * 360;
227             if(pageX < 0) pageX = 0;
228             else if(pageX > 360) pageX = 360;
229             if(this.hsv.h !== pageX){
230                 this.hsv.h = pageX;
231                 this.rgb.setFormHSV(this.hsv.h, this.hsv.s, this.hsv.v);
232                 if(typeof this.onchange === "function") this.onchange(this.value);
233                 this.updateViewHValue();
234                 this.updateViewColorInfo();
235                 this.updateViewSVColor();
236                 this.updateViewHCursor();
237                 this.redraw();
238             }
239 
240         },
241 
242         onMoveH = event => {
243             setH(event.pageX);
244         },
245 
246         onUpH = () => {
247             document.body.removeEventListener("pointerup", onUpH);
248             document.body.removeEventListener("pointermove", onMoveH);
249             cae.remove(this.viewHCursor, 'up', onUpH);
250             cae.remove(this.viewHCursor, 'move', onMoveH);
251 
252         },
253 
254         onDownH = event => {
255             setH(event.pageX);
256             cae.add(this.viewHCursor, "up", onUpH);
257             cae.add(this.viewHCursor, "move", onMoveH);
258             document.body.addEventListener("pointerup", onUpH);
259             document.body.addEventListener("pointermove", onMoveH);
260 
261         }
262         
263         cae.add(this.viewHScroll, "down", onDownH);
264         cae.add(this.viewHCursor, "down", onDownH);
265         this.onUpH = onUpH;
266 
267 
268 
269         //A
270         const setA = (pageX) => {
271             pageX = (pageX - this.domElementRect.x - this.viewAScroll.box.x) / this.viewAScroll.box.w;
272             if(pageX < 0) pageX = 0;
273             else if(pageX > 1) pageX = 1;
274             if(this.alpha !== pageX){
275                 this.alpha = pageX;
276                 if(typeof this.onchange === "function") this.onchange(this.value);
277                 this.updateViewColorInfo();
278                 this.updateViewHValue();
279                 this.updateViewACursor();
280                 this.redraw();
281             }
282 
283         },
284 
285         onMoveA = event => {
286             setA(event.pageX);
287         },
288 
289         onUpA = () => {
290             document.body.removeEventListener("pointerup", onUpA);
291             document.body.removeEventListener("pointermove", onMoveA);
292             cae.remove(this.viewACursor, 'up', onUpA);
293             cae.remove(this.viewACursor, 'move', onMoveA);
294             
295         },
296 
297         onDownA = event => {
298             setA(event.pageX);
299             cae.add(this.viewACursor, "up", onUpA);
300             cae.add(this.viewACursor, "move", onMoveA);
301             document.body.addEventListener("pointerup", onUpA);
302             document.body.addEventListener("pointermove", onMoveA);
303 
304         }
305 
306         cae.add(this.viewAScroll, "down", onDownA);
307         cae.add(this.viewACursor, "down", onDownA);
308         this.onUpA = onUpA;
309 
310         this.cae = cae;
311 
312     }
313 
314 
315     //SV 明度 與 灰度
316     updateViewSVCursor(){
317         this.viewSVCursor.pos(this.hsv.s / 100 * this.viewSVColor.box.w + this.viewSVColor.box.x - this.viewSVCursor.circle.r, (1 - this.hsv.v / 100) * this.viewSVColor.box.h + this.viewSVColor.box.y - this.viewSVCursor.circle.r);
318     }
319 
320     updateViewSVColor(){
321         this.viewSVColor.clear().fill(ColorTestViewer.emptyColor.setFormHSV(this.hsv.h, 100, 100).getHexString());
322 
323     }
324 
325     setViewSVPos(x, y){
326         this.viewSVColor.pos(x, y);
327         this.viewSVsv.pos(x, y);
328         this.updateViewSVCursor();
329     }
330 
331     initViewSVColor(width, height){ //*3
332         this.viewSVColor = new CanvasAnimateCustom().size(width, height).rect();
333 
334         this.viewSVsv = new CanvasAnimateCustom().size(width, height);
335         const gradientS = this.viewSVsv.linearGradient(0, height, width, height, ColorTestViewer.colorS, true),
336         gradientV = this.viewSVsv.linearGradient(width, height, width, 0, ColorTestViewer.colorV, true);
337         this.viewSVsv.rect().fill(gradientS).fill(gradientV);
338 
339         this.viewSVCursor = new CanvasAnimateCustom().size(10, 10);
340         this.viewSVCursor.computeCircle();
341         this.viewSVCursor.arc().stroke("#fff");
342 
343         this.list.push(this.viewSVColor, this.viewSVsv, this.viewSVCursor);
344 
345         this.setViewSVPos(0, 0);
346         this.updateViewSVColor();
347 
348     }
349 
350 
351     //H 顏色
352     updateViewHValue(){
353         this.viewHValue.clear().fill(this.rgb.getRGBA(this.alpha));
354 
355     }
356 
357     setViewHValuePos(x, y){
358         this.viewHValueBG.pos(x, y);
359         this.viewHValue.pos(x, y);
360 
361     }
362 
363     initViewHValue(width, height){ //*2
364         this.viewHValueBG = new CanvasAnimateCustom().size(width, height)
365         .drawTransparentBG(5, 0, 0, width, height);
366 
367         this.viewHValue = new CanvasAnimateCustom().size(width, height)
368         .rect();
369 
370         this.list.push(this.viewHValueBG, this.viewHValue);
371         this.updateViewHValue();
372 
373     }
374 
375     updateViewHCursor(){
376         this.viewHCursor.pos(this.hsv.h / 360 * this.viewHScroll.box.w + this.viewHScroll.box.x - this.viewHCursor.circle.r, this.viewHScroll.box.y);
377 
378     }
379 
380     setViewHScrollPos(x, y){
381         this.viewHScroll.pos(x, y);
382         this.updateViewHCursor();
383     }
384 
385     initViewHScroll(width, height){ //*2
386         this.viewHScroll = new CanvasAnimateCustom().size(width, height).rect();
387         this.viewHScroll.fill(this.viewHScroll.linearGradient(0, height, width, height, ColorTestViewer.colorH, true));
388 
389         const size = Math.min(width, height);
390         this.viewHCursor = new CanvasAnimateCustom().size(size, size);
391         this.viewHCursor.computeCircle();
392         this.viewHCursor.arc().stroke("#fff");
393         
394         this.list.push(this.viewHScroll, this.viewHCursor);
395         this.setViewHScrollPos(0, 0);
396 
397     }
398 
399 
400     //A 透明度
401     updateViewACursor(){
402         this.viewACursor.pos(this.alpha * this.viewAScroll.box.w + this.viewAScroll.box.x - this.viewACursor.circle.r, this.viewAScroll.box.y);
403 
404     }
405 
406     setViewAScrollPos(x, y){
407         this.viewAScroll.pos(x, y);
408         this.updateViewACursor();
409     }
410 
411     initViewAScroll(width, height){ //*2
412         this.viewAScroll = new CanvasAnimateCustom().size(width, height)
413         .drawTransparentBG(5, 0, 0, width, height).rect();
414         this.viewAScroll.fill(this.viewAScroll.linearGradient(0, height, width, height, ColorTestViewer.colorA));
415 
416         const size = Math.min(width, height);
417         this.viewACursor = new CanvasAnimateCustom().size(size, size);
418         this.viewACursor.computeCircle();
419         this.viewACursor.arc().stroke("rgb(0,160,255)");
420 
421         this.list.push(this.viewAScroll, this.viewACursor);
422         this.setViewAScrollPos(0, 0);
423 
424

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 記錄一篇.netwebapi開發過程 首先使用6+dapper+sqlserver 我們創建一個空項目使用HttpReports來監控api HttpReports 基於.Net Core 開發的APM監控系統,使用MIT開源協議,主要功能包括,統計, 分析, 可視化, 監控,追蹤等,適合在微服務環 ...
  • 痞子衡嵌入式半月刊: 第 55 期 這裡分享嵌入式領域有用有趣的項目/工具以及一些熱點新聞,農曆年分二十四節氣,希望在每個交節之日準時發佈一期。 本期刊是開源項目(GitHub: JayHeng/pzh-mcu-bi-weekly),歡迎提交 issue,投稿或推薦你知道的嵌入式那些事兒。 上期回顧 ...
  • 寫在前面 它們不厭其煩地執行人的指令;它們收集世間萬物的知識,供人頃刻之間隨心調取;它們是現代社會的中流砥柱,但其存在卻往往備受忽視。 它們就是電腦,是人類迄今為止最偉大的發明成就,是登峰造極、至高無上的終極工具。 電腦科學的問世,推動了人類歷史上最非比尋常的社會變革之一。 而編程語言作為電腦 ...
  • 導讀: 本文將介紹過去15年中,網易大數據團隊在應對不斷涌現的新需求、新痛點的過程中,逐漸形成的一套邏輯數據湖落地方法。內容分為五部分: 關於網易數帆 為什麼做邏輯數據湖 怎麼做邏輯數據湖 未來規劃 精彩問答 -- 01 關於網易數帆 網易數帆是從網易杭州研究院孵化出來的。網易杭研的重要職責是公共技 ...
  • Buffer pool 我們都知道我們讀取頁面是需要將其從磁碟中讀到記憶體中,然後等待CPU對數據進行處理。我們直到從磁碟中讀取數據到記憶體的過程是十分慢的,所以我們讀取的頁面需要將其緩存起來,所以MySQL有這個buffer pool對頁面進行緩存。 首先MySQL在啟動時會向操作系統申請一段連續的內 ...
  • 原文地址: 關於TornadoFx和Android的全局配置工具類封裝實現及思路解析 - Stars-One的雜貨小窩 目前個人開發軟體存在設置頁面,可以讓用戶自定義些設置,但我發現,存儲數據的代碼邏輯實在是有些繁瑣(保存及APP打開的設置初始化) 於是便是花了些精力研究了些,封裝了個簡單的工具類, ...
  • Vue框架搭建項目時所用的vue官方項目模版,通過腳手架,下載vue項目,如何創建項目,認識項目結構和拉取項目模版。 ...
  • DOM獲取元素、修改元素 1.DOM ①什麼是DOM?作用? DOM是文檔對象模型作用:操作網頁內容,可以開髮網頁內容特效和實現用戶交互。 ②DOM對象 2.獲取DOM元素 ① 根據CSS選擇器來獲取DOM元素 (重點) 思考 獲取一個DOM元素我們使用誰?querySelector() 獲取多個D ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...