From c447fb97c302101dbea12f6d482cde12f2024598 Mon Sep 17 00:00:00 2001 From: shutaozhenzhen <3462232313@qq.com> Date: Fri, 7 Dec 2018 23:34:43 +0800 Subject: [PATCH] Add files via upload --- box/IconMake/README.md | 12 +- box/IconMake/main.js | 154 +--------------- box/IconMake/scripts/UI/mainViews.js | 18 ++ box/IconMake/scripts/const.js | 200 +++++++++++++++++++++ box/IconMake/scripts/const/sideLength.js | 3 + box/IconMake/scripts/layouts/viewLayout.js | 7 + 6 files changed, 237 insertions(+), 157 deletions(-) create mode 100644 box/IconMake/scripts/UI/mainViews.js create mode 100644 box/IconMake/scripts/const.js create mode 100644 box/IconMake/scripts/const/sideLength.js create mode 100644 box/IconMake/scripts/layouts/viewLayout.js diff --git a/box/IconMake/README.md b/box/IconMake/README.md index 921286c..b112802 100644 --- a/box/IconMake/README.md +++ b/box/IconMake/README.md @@ -1,10 +1,14 @@ 18/12/07 - 0.0.2 -- 将非安装包格式转移至安装包格式,尝试重构ui和代码 -- 字符本地化,函数改善。(修改compare函数以准备未来从input或slider输入颜色) + +* 将非安装包格式转移至安装包格式,尝试重构ui和代码 +* 字符本地化,函数改善。(修改compare函数以准备未来从input或slider输入颜色) +* //$ui.alert($file.list("shared://../Icons")) 0.0.3 -- 本地化字符 +* 本地化字符 + +0.0.4 +* 分离常量 diff --git a/box/IconMake/main.js b/box/IconMake/main.js index 19470a3..10531ec 100644 --- a/box/IconMake/main.js +++ b/box/IconMake/main.js @@ -1,157 +1,5 @@ -//$ui.alert($file.list("shared://../Icons")) -let image const - sideLength = Math.ceil(72.0 / $device.info.screen.scale), - view = { - type: "view", - props: { - id: "viewMain" - }, - layout: function (make, view) { - make.center.equalTo(view.super) - make.size.equalTo($size(sideLength, sideLength)) - } - }, - getSnapshot = function () { - return $("viewMain").snapshot; - }, - compare = function (imageT, colorC) { - let - colorCComponents = colorC.components, - red = colorCComponents.red, - green = colorCComponents.green, - blue = colorCComponents.blue, - sum = red + green + blue; - return function (w, h) { - let color = imageT.colorAtPixel($point(w, h)), - components = color.components, - r = components.red, - g = components.green, - b = components.blue, - sumT = r + g + b; - if (sumT > sum) { - return true - } else { - return false - } - } - }, - compareAvg = function (imageT) { - return compare(imageT, imageT.averageColor); - }, - buttonSave = { - type: "button", - props: { - title: $l10n("save") - }, - layout: function (make, view) { - make.centerX.equalTo(view.super).multipliedBy(1.4) - make.centerY.equalTo(view.super).multipliedBy(1.5) - make.width.equalTo(view.super).multipliedBy(0.15) - }, - events: { - tapped: function () { - let data = $("canvasView").snapshot.resized($size(72, 72)).png, - path = "shared://iconMake/" - if ($file.isDirectory(path)) { - - } else { - $file.mkdir(path) - } - var success = $file.write({ - data: data, - path: path + "icon.png" - }) - if (success) { - $ui.alert({ - title: "", - message: "存储成功,地址:\n" + path + "icon.png", - }); - } else { - $ui.alert({ - title: "", - message: "存储失败", - }); - } - //$quicklook.open({ image: $("canvasView").snapshot }); - } - } - }, - buttonSelect = { - type: "button", - props: { - title: $l10n("select") - }, - layout: function (make, view) { - make.centerX.equalTo(view.super).multipliedBy(0.6) - make.centerY.equalTo(view.super).multipliedBy(1.5) - make.width.equalTo(view.super).multipliedBy(0.15) - }, - events: { - tapped: function () { - if ($("canvasView")) { - $("canvasView").remove() - } - - let viewMain = $("viewMain") - $photo.pick().then(function (resp) { - if (resp.image) { - image = resp.image; - viewMain.add({ - type: "canvas", - props: { - id: "canvasView" - }, - layout: $layout.fill, - events: { - draw: function (view, ctx) { - ctx.drawImage(view.frame, image) - } - } - }) - image = getSnapshot() - $("canvasView").remove() - viewMain.add({ - type: "canvas", - props: { - id: "canvasView" - }, - layout: $layout.fill, - events: { - draw: function (view, ctx) { - let gray = $color("gray"), - clear = $color("clear") - let compareWH = compareAvg(image) - for (let w = 0; w < sideLength; w++) { - for (let h = 0; h < sideLength; h++) { - if (compareWH(w, h)) { - ctx.fillColor = gray; - } else { - ctx.fillColor = clear; - } - ctx.fillRect($rect(w, h, 1, 1)) - } - } - } - } - }) - $quicklook.open({ image: getSnapshot() }); - } else { - $ui.alert({ - title: "提醒", - message: "你没有选择图片", - }); - $app.close(); - } - }); - } - } - }, - mainViews = [ - view, - buttonSave, - buttonSelect - ], + mainViews = require("./scripts/const.js"), ui = { type: "view", props: { diff --git a/box/IconMake/scripts/UI/mainViews.js b/box/IconMake/scripts/UI/mainViews.js new file mode 100644 index 0000000..fc1d8c1 --- /dev/null +++ b/box/IconMake/scripts/UI/mainViews.js @@ -0,0 +1,18 @@ +const + sideLength = require("scripts/const/sideLength.js"), + view = { + type: "view", + props: { + id: "viewMain" + }, + layout: function (make, view) { + make.center.equalTo(view.super) + make.size.equalTo($size(sideLength, sideLength)) + } + }, + mainViews = [ + view, + buttonSave, + buttonSelect + ] +module.exports = mainViews \ No newline at end of file diff --git a/box/IconMake/scripts/const.js b/box/IconMake/scripts/const.js new file mode 100644 index 0000000..f2805ce --- /dev/null +++ b/box/IconMake/scripts/const.js @@ -0,0 +1,200 @@ +let image +const + viewLayout = require("./layouts/viewLayout.js"), + view = { + type: "view", + props: { + id: "viewMain" + }, + layout: viewLayout + } +const + sideLength = require("scripts/const/sideLength.js"), + getSnapshot = (id) => { + return $(id).snapshot; + }, + sumId = (color) => { + let components = color.components, + r = components.red, + g = components.green, + b = components.blue, + sum = r + g + b; + return sum; + }, + maxId = (color) => { + let components = color.components, + r = components.red, + g = components.green, + b = components.blue, + max = Math.max(r, g, b); + return max; + }, + avgId = (color) => { + let components = color.components, + r = components.red, + g = components.green, + b = components.blue, + sum = r + g + b, + avg = sum / 3; + return avg; + }, + minId = (color) => { + let components = color.components, + r = components.red, + g = components.green, + b = components.blue, + min = Math.min(r, g, b); + return min; + }, + grayId = (color) => { + let components = color.components, + r = components.red, + g = components.green, + b = components.blue, + gray = (r * 0.299 + g * 0.587 + b * 0.114) + return gray; + }, + saturationId = (color) => { + let components = color.components, + r = components.red, + g = components.green, + b = components.blue, + max = Math.max(r, g, b), + min = Math.min(r, g, b); + return max ? (1 - min / max) : max; + }, + compareByFunc = (idFunc) => { + return (imageT, colorC) => { + let + id = idFunc(colorC); + return function (w, h) { + let color = imageT.colorAtPixel($point(w, h)), + idT = idFunc(color) + if (idT > id) { + return true + } else { + return false + } + } + } + }, + compareAvgBySum = function (imageT) { + return compareByFunc(sumId)(imageT, imageT.averageColor); + }, + compareAvgByMax = function (imageT) { + return compareByFunc(maxId)(imageT, imageT.averageColor); + }, + buttonSave = { + type: "button", + props: { + title: $l10n("save") + }, + layout: function (make, view) { + make.centerX.equalTo(view.super).multipliedBy(1.4) + make.centerY.equalTo(view.super).multipliedBy(1.5) + make.width.equalTo(view.super).multipliedBy(0.15) + }, + events: { + tapped: function () { + let data = $("canvasView").snapshot.resized($size(72, 72)).png, + path = "shared://iconMake/" + if ($file.isDirectory(path)) { + + } else { + $file.mkdir(path) + } + var success = $file.write({ + data: data, + path: path + "icon.png" + }) + if (success) { + $ui.alert({ + title: "", + message: "存储成功,地址:\n" + path + "icon.png", + }); + } else { + $ui.alert({ + title: "", + message: "存储失败", + }); + } + //$quicklook.open({ image: $("canvasView").snapshot }); + } + } + }, + buttonSelect = { + type: "button", + props: { + title: $l10n("select") + }, + layout: function (make, view) { + make.centerX.equalTo(view.super).multipliedBy(0.6) + make.centerY.equalTo(view.super).multipliedBy(1.5) + make.width.equalTo(view.super).multipliedBy(0.15) + }, + events: { + tapped: function () { + if ($("canvasView")) { + $("canvasView").remove() + } + + let viewMain = $("viewMain") + $photo.pick().then(function (resp) { + if (resp.image) { + image = resp.image; + viewMain.add({ + type: "canvas", + props: { + id: "canvasView" + }, + layout: $layout.fill, + events: { + draw: function (view, ctx) { + ctx.drawImage(view.frame, image) + } + } + }) + image = getSnapshot("viewMain") + $("canvasView").remove() + viewMain.add({ + type: "canvas", + props: { + id: "canvasView" + }, + layout: $layout.fill, + events: { + draw: function (view, ctx) { + let gray = $color("gray"), + clear = $color("clear") + let compareWH = compareAvgBySum(image) + for (let w = 0; w < sideLength; w++) { + for (let h = 0; h < sideLength; h++) { + if (compareWH(w, h)) { + ctx.fillColor = gray; + } else { + ctx.fillColor = clear; + } + ctx.fillRect($rect(w, h, 1, 1)) + } + } + } + } + }) + $quicklook.open({ image: getSnapshot("viewMain") }); + } else { + $ui.alert({ + title: "提醒", + message: "你没有选择图片", + }); + $app.close(); + } + }); + } + } + }, + mainViews = [ + view, + buttonSave, + buttonSelect + ] +module.exports = mainViews \ No newline at end of file diff --git a/box/IconMake/scripts/const/sideLength.js b/box/IconMake/scripts/const/sideLength.js new file mode 100644 index 0000000..6c2171f --- /dev/null +++ b/box/IconMake/scripts/const/sideLength.js @@ -0,0 +1,3 @@ +const + sideLength = Math.ceil(72.0 / $device.info.screen.scale) +module.exports = sideLength \ No newline at end of file diff --git a/box/IconMake/scripts/layouts/viewLayout.js b/box/IconMake/scripts/layouts/viewLayout.js new file mode 100644 index 0000000..2a32f73 --- /dev/null +++ b/box/IconMake/scripts/layouts/viewLayout.js @@ -0,0 +1,7 @@ +const + sideLength = require("scripts/const/sideLength.js"), + viewLayout = (make, view) => { + make.center.equalTo(view.super) + make.size.equalTo($size(sideLength, sideLength)) + } +module.exports = viewLayout \ No newline at end of file