diff --git a/frameworks/common/include/sec_comp_tool.h b/frameworks/common/include/sec_comp_tool.h index 5a4417231caf1d4172d5ca11442c026e6a64720a..2ae4598509a2e363ce4359b238477798cdf64b60 100644 --- a/frameworks/common/include/sec_comp_tool.h +++ b/frameworks/common/include/sec_comp_tool.h @@ -20,7 +20,6 @@ namespace OHOS { namespace Security { namespace SecurityComponent { - bool IsColorSimilar(const SecCompColor& color1, const SecCompColor& color2); bool IsColorTransparent(const SecCompColor& color); bool IsColorFullTransparent(const SecCompColor& color); } // namespace SecurityComponent diff --git a/frameworks/common/src/sec_comp_tool.cpp b/frameworks/common/src/sec_comp_tool.cpp index 4949b035e0783103718eb77b540292e71df17938..9856b07ca349e50f86741038b456f8de07174794 100644 --- a/frameworks/common/src/sec_comp_tool.cpp +++ b/frameworks/common/src/sec_comp_tool.cpp @@ -22,132 +22,11 @@ namespace OHOS { namespace Security { namespace SecurityComponent { namespace { -static constexpr double PI_CIRCLE = 3.1415926; -static constexpr double MIN_HSV_DISTANCE = 25.0; -static constexpr double MAX_RGB_VALUE = 255.0; static constexpr uint8_t MAX_TRANSPARENT = 0x99; // 60% -static constexpr double ZERO_DOUBLE = 0.0; -static constexpr double THIRTY_ANGLE = 30.0; -static constexpr double SIXTY_ANGLE = 60.0; -static constexpr double ONE_HUNDRED_TWEENTY_ANGLE = 120.0; -static constexpr double ONE_HUNDRED_EIGHTY_ANGLE = 180.0; -static constexpr double TWO_HUNDREDS_FORTY_ANGLE = 240.0; -static constexpr double THREE_HUNDREDS_SIXTY_ANGLE = 360.0; -static constexpr double DEFAULT_R = 100.0; -static const uint8_t MAX_ALPHA = 0xFF; -static const double MIN_CONTRAST_ALPHA = 0.5; constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_SECURITY_COMPONENT, "SecCompTool"}; } -static inline double MaxValue(double a, double b, double c) -{ - return ((a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c)); -} - -static inline double MinValue(double a, double b, double c) -{ - return ((a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c)); -} - -struct HsvColor { - double h; - double s; - double v; -}; - -static HsvColor RgbToHsv(uint8_t r, uint8_t g, uint8_t b) -{ - HsvColor hsv; - double red = static_cast(r) / MAX_RGB_VALUE; - double green = static_cast(g) / MAX_RGB_VALUE; - double blue = static_cast(b) / MAX_RGB_VALUE; - double max = MaxValue(red, green, blue); - double min = MinValue(red, green, blue); - double delta = max - min; - if (max == min) { - hsv.h = ZERO_DOUBLE; - } else if (max == red) { - hsv.h = fmod((SIXTY_ANGLE * ((green - blue) / delta) + THREE_HUNDREDS_SIXTY_ANGLE), - THREE_HUNDREDS_SIXTY_ANGLE); - } else if (max == green) { - hsv.h = fmod((SIXTY_ANGLE * ((blue - red) / delta) + ONE_HUNDRED_TWEENTY_ANGLE), THREE_HUNDREDS_SIXTY_ANGLE); - } else if (max == blue) { - hsv.h = fmod((SIXTY_ANGLE * ((red - green) / delta) + TWO_HUNDREDS_FORTY_ANGLE), THREE_HUNDREDS_SIXTY_ANGLE); - } - - if (max == 0) { - hsv.s = ZERO_DOUBLE; - } else { - hsv.s = delta / max; - } - - hsv.v = max; - return hsv; -} - -static inline double GetHsvDisX(const HsvColor& hsv, double r) -{ - return r * hsv.v * hsv.s * cos(hsv.h / ONE_HUNDRED_EIGHTY_ANGLE * PI_CIRCLE); -} - -static inline double GetHsvDisY(const HsvColor& hsv, double r) -{ - return r * hsv.v * hsv.s * sin(hsv.h / ONE_HUNDRED_EIGHTY_ANGLE * PI_CIRCLE); -} - -static inline double GetHsvDisZ(const HsvColor& hsv, double h) -{ - return h * (1 - hsv.v); -} - -static double HsvDistance(const HsvColor& hsv1, const HsvColor& hsv2) -{ - double rDef = DEFAULT_R; - double angle = THIRTY_ANGLE; - double h = rDef * cos(angle / ONE_HUNDRED_EIGHTY_ANGLE * PI_CIRCLE); - double r = rDef * sin(angle / ONE_HUNDRED_EIGHTY_ANGLE * PI_CIRCLE); - - double x1 = GetHsvDisX(hsv1, r); - double y1 = GetHsvDisY(hsv1, r); - double z1 = GetHsvDisZ(hsv1, h); - - double x2 = GetHsvDisX(hsv2, r); - double y2 = GetHsvDisY(hsv2, r); - double z2 = GetHsvDisZ(hsv2, h); - - double dx = x1 - x2; - double dy = y1 - y2; - double dz = z1 - z2; - - return sqrt((dx * dx) + (dy * dy) + (dz * dz)); -} - -static bool IsColorAplhaSimilar(const SecCompColor& fgColor, const SecCompColor& bgColor) -{ - double fgAlpha = static_cast(fgColor.argb.alpha) / MAX_ALPHA; - double bgAlpha = static_cast(bgColor.argb.alpha) / MAX_ALPHA; - - double mixAlpha = fgAlpha + bgAlpha - fgAlpha * bgAlpha; - if (IsEqual(mixAlpha, ZERO_DOUBLE) || GreatNotEqual(bgAlpha / mixAlpha, MIN_CONTRAST_ALPHA)) { - SC_LOG_ERROR(LABEL, "FgAlpha=%{public}x BgAlpha=%{public}x is similar, check failed", - fgColor.argb.alpha, bgColor.argb.alpha); - return true; - } - return false; -} - -bool IsColorSimilar(const SecCompColor& color1, const SecCompColor& color2) -{ - HsvColor hsv1 = RgbToHsv(color1.argb.red, color1.argb.green, color1.argb.blue); - HsvColor hsv2 = RgbToHsv(color2.argb.red, color2.argb.green, color2.argb.blue); - - double distance = HsvDistance(hsv1, hsv2); - SC_LOG_DEBUG(LABEL, "Compare color %{public}x %{public}x distance %{public}f", - color1.value, color2.value, distance); - return (distance < MIN_HSV_DISTANCE) && IsColorAplhaSimilar(color1, color2); -} - bool IsColorTransparent(const SecCompColor& color) { SC_LOG_DEBUG(LABEL, "Color %{public}x alpha %{public}x", color.value, color.argb.alpha); diff --git a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp index 6c113a238fd63e7b4688bed4def0008a8625a5dc..ad68957069124009cd2785ee1a4d4a5a6e5e78bb 100644 --- a/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp +++ b/services/security_component_service/sa/sa_main/sec_comp_info_helper.cpp @@ -239,16 +239,16 @@ std::string ColorToHexString(const SecCompColor& color) static bool CheckSecCompBaseButtonColorsimilar(const SecCompBase* comp, std::string& message) { if ((comp->bg_ != SecCompBackground::NO_BG_TYPE) && !IsColorFullTransparent(comp->bgColor_) && - (comp->icon_ != NO_ICON) && IsColorSimilar(comp->iconColor_, comp->bgColor_)) { - SC_LOG_INFO(LABEL, "SecurityComponentCheckFail: iconColor is similar with backgroundColor."); + (comp->icon_ != NO_ICON) && (comp->iconColor_.value == comp->bgColor_.value)) { + SC_LOG_INFO(LABEL, "SecurityComponentCheckFail: iconColor is the same with backgroundColor."); message = ", icon color is similar with background color, icon color = " + ColorToHexString(comp->iconColor_) + ", background color = " + ColorToHexString(comp->bgColor_); return false; } if ((comp->bg_ != SecCompBackground::NO_BG_TYPE) && !IsColorFullTransparent(comp->bgColor_) && - (comp->text_ != NO_TEXT) && IsColorSimilar(comp->fontColor_, comp->bgColor_)) { - SC_LOG_INFO(LABEL, "SecurityComponentCheckFail: fontColor is similar with backgroundColor."); + (comp->text_ != NO_TEXT) && (comp->fontColor_.value == comp->bgColor_.value)) { + SC_LOG_INFO(LABEL, "SecurityComponentCheckFail: fontColor is the same with backgroundColor."); message = ", font color is similar with background color, font color = " + ColorToHexString(comp->fontColor_) + ", background color = " + ColorToHexString(comp->bgColor_); return false; diff --git a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp index 60e2b315b9a7805fe61da52fda928a68d896e774..8798760ee8485f35133dd45acd9297fafc6f6183 100644 --- a/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp +++ b/services/security_component_service/sa/test/unittest/src/sec_comp_info_helper_test.cpp @@ -634,34 +634,6 @@ HWTEST_F(SecCompInfoHelperTest, CheckComponentValid005, TestSize.Level0) ASSERT_TRUE(SecCompInfoHelper::CheckComponentValid(comp, message)); } -/** - * @tc.name: IsColorSimilar001 - * @tc.desc: Test IsColorSimilar - * @tc.type: FUNC - * @tc.require: - */ -HWTEST_F(SecCompInfoHelperTest, IsColorSimilar001, TestSize.Level0) -{ - SecCompColor color1 = { - .argb = { - .red = 0xFF, - .green = 0xFF, - .blue = 0xFF, - .alpha = 0xFF, - } - }; - - SecCompColor color2 = { - .argb = { - .red = 0xFF, - .green = 0xFF, - .blue = 0xFF, - .alpha = 0xF0, // different alpha - } - }; - EXPECT_TRUE(IsColorSimilar(color1, color2)); -} - /** * @tc.name: AdjustSecCompRect001 * @tc.desc: Test AdjustSecCompRect