
//----------cookie get set delete----------Start
function getCookie(name) {
    const value = `; ${document.cookie}`;
    const parts = value.split(`; ${name}=`);
    if (parts.length === 2) {
        let result = parts.pop().split(';').shift();
        console.log(result);
        return result;
    }
}

function setCookie(name, value, minute) {
    var expires = "";
    if (minute) {
        var date = new Date();
        date.setTime(date.getTime() + (minute * 60 * 1000));
        expires = "; expires=" + date.toUTCString();
    }
    var cookie = name + "=" + (value || "") + expires + "; path=/";
    document.cookie = cookie;
}

function deleteCookie(name) {
    document.cookie = name + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}
//----------cookie get set delete----------END

//----------formatDate----------Start
function formatDate(date) {
    var dateString = new Date(date.getTime() - (date.getTimezoneOffset() * 60000))
        .toISOString()
        .split("T")[0];
    return dateString;
}
Date.prototype.setFormat = function (format) {
    let year = this.getFullYear();
    let month = this.getMonth() + 1;
    let day = this.getDate();
    if (format == "yyyy-MM-dd")
        return year + '-' + (month < 10 ? '0' : '') + month + '-' + (day < 10 ? '0' : '') + day;
    if (format == "yyyy/MM/dd")
        return year + '/' + (month < 10 ? '0' : '') + month + '/' + (day < 10 ? '0' : '') + day;
}

Date.prototype.addMonth = function (num) {
    let year = this.getFullYear();
    let month = this.getMonth() + num;
    let day = this.getDate();
    return new Date(year, month, day);
}
//----------formatDate----------END

//----------通知 字首icon function----------Start
function TextColor(text) {
    let charArray = text.split("");
    let total = 0;
    for (let i in charArray) {
        total += charArray[i].codePointAt(0)
    }
    let f = parseFloat("0." + total);

    return "#" + ((1 << 24) * f | 0).toString(16);
}
//16進制顏色轉化為RGB顏色
function colorRgb(str) {
    var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
    var sColor = str.toLowerCase();
    if (sColor && reg.test(sColor)) {
        if (sColor.length === 4) {
            var sColorNew = "#";
            for (var i = 1; i < 4; i += 1) {
                sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1));
            }
            sColor = sColorNew;
        }
        //處理六位的顏色值
        var sColorChange = [];
        for (var i = 1; i < 7; i += 2) {
            sColorChange.push(parseInt("0x" + sColor.slice(i, i + 2)));
        }
        return "rgb(" + sColorChange.join(",") + ")";
    } else {
        return sColor;
    }
}
//判斷是否為亮色
function getContrastYIQ(hexcolor) {
    var colorrgb = colorRgb(hexcolor);
    var colors = colorrgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
    var red = colors[1];
    var green = colors[2];
    var blue = colors[3];
    var brightness;
    brightness = (red * 299) + (green * 587) + (blue * 114);
    brightness = brightness / 255000;
    if (brightness >= 0.5) {
        return "light";
    } else {
        return "dark";
    }
}
//----------通知 字首icon function----------END
