Browse Source

refactor(color): 重构颜色对比度计算功能

- 将颜色对比度计算功能从 GameRewardGroupController 移至单独的 Color 类中
- 更新文档,移除宝箱价格信息
- 修正 Herlper 类中的 getContrastColor 方法
notfff 6 months ago
parent
commit
9532c16805

+ 17 - 0
UCore/DcatAdmin/Herlper.php

@@ -5,4 +5,21 @@ namespace UCore\DcatAdmin;
 class Herlper
 {
 
+
+    /**
+     * 根据背景色获取对比色
+     *
+     * @param string $hexColor
+     * @return string
+     */
+    public static function getContrastColor(string $hexColor): string
+    {
+        $hexColor = ltrim($hexColor, '#');
+        $r = hexdec(substr($hexColor, 0, 2));
+        $g = hexdec(substr($hexColor, 2, 2));
+        $b = hexdec(substr($hexColor, 4, 2));
+        $brightness = ($r * 299 + $g * 587 + $b * 114) / 1000;
+        return $brightness > 128 ? '#000000' : '#ffffff';
+    }
+
 }

+ 24 - 0
UCore/Helper/Color.php

@@ -0,0 +1,24 @@
+<?php
+
+namespace UCore\Helper;
+
+class Color
+{
+    /**
+     * 根据背景色获取对比色
+     *
+     * @param string $hexColor
+     * @return string
+     */
+    public static function getContrastColor(string $hexColor): string
+    {
+        $hexColor = ltrim($hexColor, '#');
+        $r = hexdec(substr($hexColor, 0, 2));
+        $g = hexdec(substr($hexColor, 2, 2));
+        $b = hexdec(substr($hexColor, 4, 2));
+        $brightness = ($r * 299 + $g * 587 + $b * 114) / 1000;
+        return $brightness > 128 ? '#000000' : '#ffffff';
+    }
+
+
+}

+ 4 - 4
app/Module/Farm/Docs/数值2.md

@@ -2,10 +2,10 @@
 ## 商城
 
 
-铜宝箱  = 100钻石 ;500萝卜 + 500辣酱开启 ,产出 200-2000萝卜 或 辣椒
-银宝箱  = 200钻石 ;500苹果 + 500 西瓜 开启 ,产出 200-2000 苹果 或 西瓜
-金宝箱  = 300钻石 ;500南瓜 + 500 草莓 开启 ,产出 200-2000 南瓜 或 草莓
-钻石宝箱  = 1000钻石 ;100苹果 + 100 草莓 西瓜 ,产出 1000-2000 钻石
+铜宝箱  = ;500萝卜 + 500辣酱开启 ,产出 200-2000萝卜 或 辣椒
+银宝箱  = ;500苹果 + 500 西瓜 开启 ,产出 200-2000 苹果 或 西瓜
+金宝箱  = ;500南瓜 + 500 草莓 开启 ,产出 200-2000 南瓜 或 草莓
+钻石宝箱  = ;100苹果 + 100 草莓 西瓜 ,产出 1000-2000 钻石
 
 除草之神 = 700钻 ,24小时
 雨露之神 = 700钻 ,24小时

+ 3 - 16
app/Module/Game/AdminControllers/GameRewardGroupController.php

@@ -17,6 +17,7 @@ use Dcat\Admin\Grid;
 use Dcat\Admin\Show;
 use Dcat\Admin\Http\Controllers\AdminController;
 use Spatie\RouteAttributes\Attributes\Resource;
+use UCore\Helper\Color;
 
 /**
  * 奖励组管理控制器
@@ -126,7 +127,7 @@ class GameRewardGroupController extends AdminController
                     $tagHtml[] = sprintf(
                         '<span class="badge" style="background-color: %s; color: %s;">%s</span>',
                         $tag['color'],
-                        $this->getContrastColor($tag['color']),
+                        Color::getContrastColor($tag['coler']),
                         $tag['name']
                     );
                 }
@@ -401,19 +402,5 @@ class GameRewardGroupController extends AdminController
         return (string)$item->quantity;
     }
 
-    /**
-     * 根据背景色获取对比色
-     *
-     * @param string $hexColor
-     * @return string
-     */
-    private function getContrastColor(string $hexColor): string
-    {
-        $hexColor = ltrim($hexColor, '#');
-        $r = hexdec(substr($hexColor, 0, 2));
-        $g = hexdec(substr($hexColor, 2, 2));
-        $b = hexdec(substr($hexColor, 4, 2));
-        $brightness = ($r * 299 + $g * 587 + $b * 114) / 1000;
-        return $brightness > 128 ? '#000000' : '#ffffff';
-    }
+
 }