Skip to content

Commit

Permalink
思考计划: 修复hexToRgb测试错误问题2
Browse files Browse the repository at this point in the history
  • Loading branch information
chenbimo committed Aug 5, 2024
1 parent 264db38 commit b5d8452
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 19 deletions.
4 changes: 4 additions & 0 deletions lib/color/_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "颜色",
"describe": "颜色转换,调整,修改等"
}
4 changes: 2 additions & 2 deletions lib/string/hexTorgb.js → lib/color/hexToRgb.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* 颜色转换
* 十六进制颜色转rgb
* @alias yd_string_hexToRgb
* @category string
* @category color
* @param {string} hex 要转换的hex颜色值
* @returns {string} rgb颜色
* @author wzskyline
Expand Down
File renamed without changes.
13 changes: 13 additions & 0 deletions lib/color/rgbToHex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* rgb转16进制颜色
* @alias yd_string_rgbTohex
* @category color
* @param {Array} rgbArr 要转换的rgv颜色值数组
* @returns {string} hex颜色
* @author wzskyline
* @example yd_string_rgbTohex([255, 0, 0]) -> #FF0000
*/
export default (rgbArr) => {
//return '#' + rgbarr.map(value => { const hex = value.toString(16); return hex.length === 1 ? '0' + hex : hex; }).join('');
return '#' + rgbArr.map((value) => ('00' + value.toString(16)).slice(-2)).join('');
};
8 changes: 4 additions & 4 deletions lib/string/rgbTohex.test.js → lib/color/rgbTohex.test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { describe, it, expect } from 'vitest';
import yd_string_rgbTohex from './rgbTohex.js';
import yd_string_rgbToHex from './rgbToHex.js';

describe('yd_string_rgbTohex', () => {
it('将RGB数组转换为十六进制颜色值', () => {
const rgbArray = [255, 0, 128];
const result = yd_string_rgbTohex(rgbArray);
const result = yd_string_rgbToHex(rgbArray);
expect(result).toBe('#ff0080');
});

it('处理RGB值小于16的情况', () => {
const rgbArray = [10, 5, 3];
const result = yd_string_rgbTohex(rgbArray);
const result = yd_string_rgbToHex(rgbArray);
expect(result).toBe('#0a0503');
});

it('处理RGB值为0的情况', () => {
const rgbArray = [0, 0, 0];
const result = yd_string_rgbTohex(rgbArray);
const result = yd_string_rgbToHex(rgbArray);
expect(result).toBe('#000000');
});
});
13 changes: 0 additions & 13 deletions lib/string/rgbTohex.js

This file was deleted.

0 comments on commit b5d8452

Please sign in to comment.