!17249 回退 drawing color参数支持hex

Merge pull request !17249 from 难过的馅/master
This commit is contained in:
openharmony_ci 2024-11-19 14:41:44 +00:00 committed by Gitee
commit 4800bc378b
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
9 changed files with 34 additions and 239 deletions

View File

@ -149,25 +149,13 @@ napi_value JsBrush::SetColor(napi_env env, napi_callback_info info)
Drawing::Color drawingColor;
if (argc == ARGC_ONE) {
bool isJsColor = false;
napi_has_named_property(env, argv[ARGC_ZERO], JSPROPERTY[0], &isJsColor);
if (isJsColor) {
int32_t argb[ARGC_FOUR] = {0};
if (!ConvertFromJsColor(env, argv[ARGC_ZERO], argb, ARGC_FOUR)) {
ROSEN_LOGE("JsBrush::SetColor Argv[0] is invalid");
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,
"Parameter verification failed. The range of color channels must be [0, 255].");
}
drawingColor = Color::ColorQuadSetARGB(argb[ARGC_ZERO], argb[ARGC_ONE], argb[ARGC_TWO], argb[ARGC_THREE]);
} else {
uint32_t hexNumber = 0;
GET_UINT32_PARAM(ARGC_ZERO, hexNumber);
uint32_t alpha = (hexNumber >> 24) & 0xFF;
uint32_t red = (hexNumber >> 16) & 0xFF;
uint32_t green = (hexNumber >> 8) & 0xFF;
uint32_t blue = hexNumber & 0xFF;
drawingColor = Color::ColorQuadSetARGB(alpha, red, green, blue);
int32_t argb[ARGC_FOUR] = {0};
if (!ConvertFromJsColor(env, argv[ARGC_ZERO], argb, ARGC_FOUR)) {
ROSEN_LOGE("JsBrush::SetColor Argv[0] is invalid");
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,
"Parameter verification failed. The range of color channels must be [0, 255].");
}
drawingColor = Color::ColorQuadSetARGB(argb[ARGC_ZERO], argb[ARGC_ONE], argb[ARGC_TWO], argb[ARGC_THREE]);
} else if (argc == ARGC_FOUR) {
int32_t alpha = 0;
GET_COLOR_PARAM(ARGC_ZERO, alpha);

View File

@ -480,27 +480,13 @@ napi_value JsCanvas::OnClear(napi_env env, napi_callback_info info)
napi_value argv[ARGC_ONE] = {nullptr};
CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_ONE);
ColorQuad color;
bool isJsColor = false;
napi_has_named_property(env, argv[ARGC_ZERO], JSPROPERTY[0], &isJsColor);
if (isJsColor) {
int32_t argb[ARGC_FOUR] = {0};
if (!ConvertFromJsColor(env, argv[ARGC_ZERO], argb, ARGC_FOUR)) {
ROSEN_LOGE("JsCanvas::SetColor Argv[0] is invalid");
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,
"Parameter verification failed. The range of color channels must be [0, 255].");
}
color = Color::ColorQuadSetARGB(argb[ARGC_ZERO], argb[ARGC_ONE], argb[ARGC_TWO], argb[ARGC_THREE]);
} else {
uint32_t hexNumber = 0;
GET_UINT32_PARAM(ARGC_ZERO, hexNumber);
uint32_t alpha = (hexNumber >> 24) & 0xFF;
uint32_t red = (hexNumber >> 16) & 0xFF;
uint32_t green = (hexNumber >> 8) & 0xFF;
uint32_t blue = hexNumber & 0xFF;
color = Color::ColorQuadSetARGB(alpha, red, green, blue);
int32_t argb[ARGC_FOUR] = {0};
if (!ConvertFromJsColor(env, argv[ARGC_ZERO], argb, ARGC_FOUR)) {
ROSEN_LOGE("JsCanvas::OnClear Argv[0] is invalid");
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,
"Parameter verification failed. The range of color channels must be [0, 255].");
}
auto color = Color::ColorQuadSetARGB(argb[ARGC_ZERO], argb[ARGC_ONE], argb[ARGC_TWO], argb[ARGC_THREE]);
JS_CALL_DRAWING_FUNC(m_canvas->Clear(color));
return nullptr;
@ -760,26 +746,13 @@ napi_value JsCanvas::OnDrawColor(napi_env env, napi_callback_info info)
CHECK_PARAM_NUMBER_WITH_OPTIONAL_PARAMS(argv, argc, ARGC_ONE, ARGC_FIVE);
if (argc == ARGC_ONE || argc == ARGC_TWO) {
bool isJsColor = false;
ColorQuad color;
napi_has_named_property(env, argv[ARGC_ZERO], JSPROPERTY[0], &isJsColor);
if (isJsColor) {
int32_t argb[ARGC_FOUR] = {0};
if (!ConvertFromJsColor(env, argv[ARGC_ZERO], argb, ARGC_FOUR)) {
ROSEN_LOGE("JsCanvas::OnDrawColor Argv[0] is invalid");
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,
"Parameter verification failed. The range of color channels must be [0, 255].");
}
color = Color::ColorQuadSetARGB(argb[ARGC_ZERO], argb[ARGC_ONE], argb[ARGC_TWO], argb[ARGC_THREE]);
} else {
uint32_t hexNumber = 0;
GET_UINT32_PARAM(ARGC_ZERO, hexNumber);
uint32_t alpha = (hexNumber >> 24) & 0xFF;
uint32_t red = (hexNumber >> 16) & 0xFF;
uint32_t green = (hexNumber >> 8) & 0xFF;
uint32_t blue = hexNumber & 0xFF;
color = Color::ColorQuadSetARGB(alpha, red, green, blue);
int32_t argb[ARGC_FOUR] = {0};
if (!ConvertFromJsColor(env, argv[ARGC_ZERO], argb, ARGC_FOUR)) {
ROSEN_LOGE("JsCanvas::OnDrawColor Argv[0] is invalid");
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,
"Parameter verification failed. The range of color channels must be [0, 255].");
}
auto color = Color::ColorQuadSetARGB(argb[ARGC_ZERO], argb[ARGC_ONE], argb[ARGC_TWO], argb[ARGC_THREE]);
if (argc == ARGC_ONE) {
DRAWING_PERFORMANCE_TEST_NAP_RETURN(nullptr);
m_canvas->DrawColor(color);

View File

@ -107,30 +107,18 @@ napi_value JsColorFilter::CreateBlendModeColorFilter(napi_env env, napi_callback
{
napi_value argv[ARGC_TWO] = {nullptr};
CHECK_PARAM_NUMBER_WITHOUT_OPTIONAL_PARAMS(argv, ARGC_TWO);
bool isJsColor = false;
ColorQuad color;
napi_has_named_property(env, argv[ARGC_ZERO], JSPROPERTY[0], &isJsColor);
if (isJsColor) {
int32_t argb[ARGC_FOUR] = {0};
if (!ConvertFromJsColor(env, argv[ARGC_ZERO], argb, ARGC_FOUR)) {
ROSEN_LOGE("JsColorFilter::CreateBlendModeColorFilter Argv[0] is invalid");
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,
"Parameter verification failed. The range of color channels must be [0, 255].");
}
color = Color::ColorQuadSetARGB(argb[ARGC_ZERO], argb[ARGC_ONE], argb[ARGC_TWO], argb[ARGC_THREE]);
} else {
uint32_t hexNumber = 0;
GET_UINT32_PARAM(ARGC_ZERO, hexNumber);
uint32_t alpha = (hexNumber >> 24) & 0xFF;
uint32_t red = (hexNumber >> 16) & 0xFF;
uint32_t green = (hexNumber >> 8) & 0xFF;
uint32_t blue = hexNumber & 0xFF;
color = Color::ColorQuadSetARGB(alpha, red, green, blue);
int32_t argb[ARGC_FOUR] = {0};
if (!ConvertFromJsColor(env, argv[ARGC_ZERO], argb, ARGC_FOUR)) {
ROSEN_LOGE("JsPen::SetColor Argv[0] is invalid");
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,
"Parameter verification failed. The range of color channels must be [0, 255].");
}
int32_t jsMode = 0;
GET_ENUM_PARAM(ARGC_ONE, jsMode, 0, static_cast<int32_t>(BlendMode::LUMINOSITY));
auto color = Color::ColorQuadSetARGB(argb[ARGC_ZERO], argb[ARGC_ONE], argb[ARGC_TWO], argb[ARGC_THREE]);
std::shared_ptr<ColorFilter> colorFilter = ColorFilter::CreateBlendModeColorFilter(color, BlendMode(jsMode));
return JsColorFilter::Create(env, colorFilter);
}

View File

@ -31,7 +31,6 @@ bool JsDrawingTestUtils::closeDrawingTest_ = true;
#endif
namespace Drawing {
const char* const JSPROPERTY[4] = {"alpha", "red", "green", "blue"};
void BindNativeFunction(napi_env env, napi_value object, const char* name, const char* moduleName, napi_callback func)
{
std::string fullName;

View File

@ -189,7 +189,6 @@ constexpr size_t ARGC_SEVEN = 7;
constexpr size_t ARGC_EIGHT = 8;
constexpr size_t ARGC_NINE = 9;
constexpr int NUMBER_TWO = 2;
extern const char* const JSPROPERTY[4];
constexpr size_t A_SHIFT = 24;
constexpr size_t R_SHIFT = 16;
constexpr size_t G_SHIFT = 8;

View File

@ -165,25 +165,13 @@ napi_value JsPen::SetColor(napi_env env, napi_callback_info info)
Drawing::Color drawingColor;
if (argc == ARGC_ONE) {
bool isJsColor = false;
napi_has_named_property(env, argv[ARGC_ZERO], JSPROPERTY[0], &isJsColor);
if (isJsColor) {
int32_t argb[ARGC_FOUR] = {0};
if (!ConvertFromJsColor(env, argv[ARGC_ZERO], argb, ARGC_FOUR)) {
ROSEN_LOGE("JsPen::SetColor Argv[0] is invalid");
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,
"Parameter verification failed. The range of color channels must be [0, 255].");
}
drawingColor = Color::ColorQuadSetARGB(argb[ARGC_ZERO], argb[ARGC_ONE], argb[ARGC_TWO], argb[ARGC_THREE]);
} else {
uint32_t hexNumber = 0;
GET_UINT32_PARAM(ARGC_ZERO, hexNumber);
uint32_t alpha = (hexNumber >> 24) & 0xFF;
uint32_t red = (hexNumber >> 16) & 0xFF;
uint32_t green = (hexNumber >> 8) & 0xFF;
uint32_t blue = hexNumber & 0xFF;
drawingColor = Color::ColorQuadSetARGB(alpha, red, green, blue);
int32_t argb[ARGC_FOUR] = {0};
if (!ConvertFromJsColor(env, argv[ARGC_ZERO], argb, ARGC_FOUR)) {
ROSEN_LOGE("JsPen::SetColor Argv[0] is invalid");
return NapiThrowError(env, DrawingErrorCode::ERROR_INVALID_PARAM,
"Parameter verification failed. The range of color channels must be [0, 255].");
}
drawingColor = Color::ColorQuadSetARGB(argb[ARGC_ZERO], argb[ARGC_ONE], argb[ARGC_TWO], argb[ARGC_THREE]);
} else if (argc == ARGC_FOUR) {
int32_t alpha = 0;
GET_COLOR_PARAM(ARGC_ZERO, alpha);

View File

@ -15,10 +15,10 @@
import {TestBase, StyleType} from './testbase';
import { CanvasDrawRect, CanvasDrawLine, CanvasDrawPath, CanvasDrawPoint, CanvasDrawImage, CanvasDrawCircle, CanvasDrawTextBlob,
CanvasDrawPixelMapMesh, CanvasDrawColor, CanvasSetColor, CanvasSetMatrix, CanvasResetMatrix, CanvasClipRoundRect, CanvasIsClipEmpty,
CanvasDrawPixelMapMesh, CanvasDrawColor, CanvasSetMatrix, CanvasResetMatrix, CanvasClipRoundRect, CanvasIsClipEmpty,
FontIsSubpixel, FontIsLinearMetrics, FontIsEmbolden, FontGetSkewX, FontGetScaleX, FontGetHinting, FontGetEdging,
BrushGetColorFilter, BrushSetImageFilter, PenGetColorFilter, PenSetImageFilter,
CreateBlurImageFilter, CreateColorImageFilter, CanvasDrawImageRect, CanvasDrawImageRectWithSrc, CanvasClipRegion,CanvasClear,
CreateBlurImageFilter, CreateColorImageFilter, CanvasDrawImageRect, CanvasDrawImageRectWithSrc, CanvasClipRegion,
CanvasDrawShadow,CanvasCreateLattice} from '../testcase/interface/canvastest';
import { PathLineTo, PathArcTo, PathQuadTo, PathCubicTo,PathClose, PathReset, PathIsClosed, PathGetPositionAndTangent, PathGetMatrix, PathBuildFromSvgString, PathConstructor } from '../testcase/interface/pathtest';
import { MatrixGetValue, MatrixPostRotate, MatrixPostTranslate, MatrixReset, MatrixGetAll, MatrixSetPolyToPoly, MatrixSetRectToRect, MatrixPreScale, MatrixPreTranslate, MatrixPreRotate, MatrixPostScale, MatrixMapPoints, MatrixMapRect } from '../testcase/interface/matrixtest';
@ -81,7 +81,6 @@ export class CaseFactory {
['canvasdrawshadow', () => {return new CanvasDrawShadow();}],
['canvascreatelattice', () => {return new CanvasCreateLattice();}],
['canvascliproundrect', () => { return new CanvasClipRoundRect(); }],
['canvasclear', () => { return new CanvasClear(); }],
['canvasdrawrect', () => { return new CanvasDrawRect(); }],
['canvasdrawcircle', () => { return new CanvasDrawCircle(); }],
['canvasdrawcolor', () => { return new CanvasDrawColor(); }],
@ -90,7 +89,6 @@ export class CaseFactory {
['canvasdrawline', () => { return new CanvasDrawLine(); }],
['canvasdrawtextblob', () => { return new CanvasDrawTextBlob(); }],
['canvasdrawimage', () => { return new CanvasDrawImage(); }],
['canvassetcolor', () => { return new CanvasSetColor(); }],
['canvasdrawimagerect', () => { return new CanvasDrawImageRect(); }],
['canvasdrawimagerectwithsrc', () => { return new CanvasDrawImageRectWithSrc(); }],
['canvasdrawsubpixelfont', () => { return new FontIsSubpixel(); }],
@ -149,7 +147,6 @@ export class CaseFactory {
['canvasdrawshadow', () => {return new CanvasDrawShadow();}],
['canvascreatelattice', () => {return new CanvasCreateLattice();}],
['canvas_cliproundrect', () => { return new CanvasClipRoundRect(); }],
['canvasclear', () => { return new CanvasClear(); }],// 1000次耗时378ms
['canvas_drawrect', () => { return new CanvasDrawRect(StyleType.DRAW_STYLE_COMPLEX); }], // 1000次耗时109ms
['canvas_drawline', () => { return new CanvasDrawLine(StyleType.DRAW_STYLE_COMPLEX); }], // 1000次耗时347ms
['canvas_drawpath', () => { return new CanvasDrawPath(StyleType.DRAW_STYLE_COMPLEX); }], // 100次耗时506ms

View File

@ -33,26 +33,6 @@ function printResults(canvas: drawing.Canvas, isPassed: Boolean) {
canvas.drawTextBlob(blob, 10, 50);
}
export class CanvasClear extends TestBase {
public constructor(){
super();
}
public OnTestFunction(canvas: drawing.Canvas): void {
canvas.clear({ alpha: 255, red: 255, green: 0, blue: 0 });
canvas.clear(0xffff0000);
}
public OnTestPerformance(canvas: drawing.Canvas) {
for (let i = 0; i < this.testCount_; i++) {
canvas.clear(0xffff0000);
}
}
}
export class CanvasDrawRect extends TestBase {
@ -1916,121 +1896,4 @@ export class CanvasClipRegion extends TestBase {
}
printResults(canvas, true);
}
}
export class CanvasSetColor extends TestBase {
public constructor() {
// 根据需求,如果与默认值不一样,请继承重写
super();
}
public OnTestFunction(canvas: drawing.Canvas) {
//接口调用,功能测试.cpu/gpu调用接口一致
{
let pen = new drawing.Pen();
let rect: common2D.Rect = { left: 0, top: 0, right: 100, bottom: 100 };
pen.setColor(0xff000000)
pen.setStrokeWidth(10)
canvas.attachPen(pen)
canvas.drawRect(rect)
canvas.detachPen()
}
{
let pen = new drawing.Pen();
let rect: common2D.Rect = { left: 0, top: 150, right: 100, bottom: 250 };
let color: common2D.Color = { alpha: 255, red: 255, green: 0, blue: 0 };
pen.setColor(color)
pen.setStrokeWidth(10)
canvas.attachPen(pen)
canvas.drawRect(rect)
canvas.detachPen()
}
{
let brush = new drawing.Brush();
let rect: common2D.Rect = { left: 110, top: 10, right: 200, bottom: 100 };
brush.setColor(0xffffaa00)
canvas.attachBrush(brush)
canvas.drawRect(rect)
canvas.detachBrush()
}
{
let brush = new drawing.Brush();
let rect: common2D.Rect = { left: 110, top: 160, right: 200, bottom: 250 };
let color: common2D.Color = { alpha: 255, red: 255, green: 170, blue: 0 };
brush.setColor(color)
canvas.attachBrush(brush)
canvas.drawRect(rect)
canvas.detachBrush()
}
{
let rect: common2D.Rect = { left: 210, top: 10, right: 300, bottom: 100 };
canvas.save()
canvas.clipRect(rect)
canvas.drawColor(0xffff0000)
canvas.restore()
}
{
let rect: common2D.Rect = { left: 210, top: 160, right: 300, bottom: 250 };
canvas.save()
canvas.clipRect(rect)
let color: common2D.Color = { alpha: 255, red: 255, green: 0, blue: 0 };
canvas.drawColor(color)
canvas.restore()
}
{
let font = new drawing.Font()
font.setSize(50)
let textBlob = drawing.TextBlob.makeFromString("hello", font, drawing.TextEncoding.TEXT_ENCODING_UTF8);
let pen = new drawing.Pen();
let color: common2D.Color = { alpha: 255, red: 0, green: 255, blue: 0 };
let shadowLayer = drawing.ShadowLayer.create(10, -3, 3, color)
pen.setShadowLayer(shadowLayer)
canvas.attachPen(pen)
canvas.drawTextBlob(textBlob, 10, 400)
canvas.detachPen()
}
{
let font = new drawing.Font()
font.setSize(50)
let textBlob = drawing.TextBlob.makeFromString("hello", font, drawing.TextEncoding.TEXT_ENCODING_UTF8);
let pen = new drawing.Pen();
let shadowLayer = drawing.ShadowLayer.create(10, -3, 3, 0xff00ff00)
pen.setShadowLayer(shadowLayer)
canvas.attachPen(pen)
canvas.drawTextBlob(textBlob, 10, 500)
canvas.detachPen()
}
{
let pen = new drawing.Pen();
let color: common2D.Color = { alpha: 255, red: 0, green: 255, blue: 0 };
let colorfilter = drawing.ColorFilter.createBlendModeColorFilter(color, drawing.BlendMode.SRC)
pen.setColorFilter(colorfilter)
pen.setStrokeWidth(10)
canvas.attachPen(pen)
canvas.drawRect({ left : 350, right : 400, top : 350, bottom : 400})
canvas.detachPen
}
{
let pen = new drawing.Pen();
let colorfilter = drawing.ColorFilter.createBlendModeColorFilter(0xff00ff00, drawing.BlendMode.SRC)
pen.setColorFilter(colorfilter)
pen.setStrokeWidth(10)
canvas.attachPen(pen)
canvas.drawRect({ left : 350, right : 400, top : 450, bottom : 500})
canvas.detachPen
}
}
}
}