1. Windows、Linux、macOS
ribbon = ''.join(f'\033[38;5;{idx}m#' for idx in range(0, 256, 1))
print(ribbon)
终端彩虹
2. 十六进制颜色映射 RGB 三基色
# 十六进制基础调色板.
based_palette = [f'{idx:02x}' for idx in range(0, 16, 1)]
# 十六进制彩色调色板.
colored_palette = [
f'{r:02x}/{g:02x}/{b:02x}'
for r in [0] + [0x5f + 40 * n for n in range(0, 5, 1)]
for g in [0] + [0x5f + 40 * n for n in range(0, 5, 1)]
for b in [0] + [0x5f + 40 * n for n in range(0, 5, 1)]
]
# 十六进制灰度调色板.
grayscale_palette = [
f'{idx:02x}/{idx:02x}/{idx:02x}'
for idx in [0x08 + 10 * n for n in range(0, 24, 1)]
]
color_256 = based_palette + colored_palette + grayscale_palette
color_dict = {color: idx for idx, color in enumerate(color_256)}
print(f"\033[38;5;{color_dict['ff/d7/5f']}m【测试颜色】\033[0m")
3. 打印 C 语言数组
格式化且带有颜色的数组
# include <stdio.h>
# include <stdlib.h>
// 打印 double 型三维数组.
void PrintDouble3D(double*** arr, int dim1, int dim2, int dim3) {
printf("\033[32m{\033[0m");
int i;
for (i = 0; i < dim1 - 1; ++i) {
printf("\033[33m{\033[0m");
int j;
for (j = 0; j < dim2 - 1; ++j) {
printf("\033[31m{\033[0m");
int k;
for (k = 0; k < dim3 - 1; ++k) {
printf("%lf, ", arr[i][j][k]);
}
printf("%lf\033[31m}\033[0m, ", arr[i][j][k]);
}
printf("\033[31m{\033[0m");
int k;
for (k = 0; k < dim3 - 1; ++k) {
printf("%lf, ", arr[i][j][k]);
}
printf("%lf\033[31m}\033[0m\033[33m}\033[0m, ", arr[i][j][k]);
}
printf("\033[33m{\033[0m");
int j;
for (j = 0; j < dim2 - 1; ++j) {
printf("\033[31m{\033[0m");
int k;
for (k = 0; k < dim3 - 1; ++k) {
printf("%lf, ", arr[i][j][k]);
}
printf("%lf\033[31m}\033[0m, ", arr[i][j][k]);
}
printf("\033[31m{\033[0m");
int k;
for (k = 0; k < dim3 - 1; ++k) {
printf("%lf, ", arr[i][j][k]);
}
printf("%lf\033[31m}\033[0m\033[33m}\033[0m\033[32m}\033[0m\n", arr[i][j][k]);
}
// 打印 double 型二维数组.
void PrintDouble2D(double** arr, int dim1, int dim2) {
for (int i = 0; i < dim1; ++i) {
for (int j = 0; j < dim2; ++j) {
printf("%lf\t", arr[i][j]);
}
printf("\n");
}
}
// 打印 double 型一维数组.
void PrintDouble1D(double* arr, int dim1) {
printf("{");
for (int i = 0; i < dim1 - 1; ++i) printf("%lf, ", arr[i]);
printf("%lf}\n", arr[dim1 - 1]);
}
int main(int argc, char* argv[], char** env) {
system("");
// 打印一维数组.
// ---------------------------------------------------
double C1[4] = {1.2, 3.4, 2.8, 12};
printf("---------------------------------------------------\n");
printf("1D Array is:\t");
PrintDouble1D(C1, 4);
printf("---------------------------------------------------\n");
// 打印二维数组.
// ---------------------------------------------------
int rows = 2;
int cols = 4;
double C2[2][4] = {
{1, 2, 3, 4.8},
{5.6, 6, 7, 8}
};
double* matrix[rows];
for (int i = 0; i < rows; ++i) matrix[i] = C2[i];
printf("2D Array is:\n");
PrintDouble2D(matrix, 2, 4);
printf("---------------------------------------------------\n");
// 打印三维数组.
// ---------------------------------------------------
int dim1 = 3, dim2 = 4, dim3 = 4;
double C[3][4][4] = {
{
{5.1, 3.5, 1.4, 0.2},
{4.9, 3.0, 1.4, 0.2},
{5.0, 3.3, 1.4, 0.2}
},
{
{4.9, 2.4, 3.3, 1.0},
{6.6, 2.9, 4.6, 1.3}
},
{
{6.5, 3.0, 5.5, 1.8},
{7.7, 3.8, 6.7, 2.2},
{7.2, 3.2, 6.0, 1.8},
{6.4, 2.8, 5.6, 2.1}
}
};
double*** ptr = (double***) malloc(dim1 * sizeof(double**));
for (int i = 0; i < dim1; ++i) {
ptr[i] = (double**) malloc(dim2 * sizeof(double*));
for (int j = 0; j < dim2; ++j) {
ptr[i][j] = (double*) malloc(dim3 * sizeof(double));
for (int k = 0; k < dim3; ++k) {
ptr[i][j][k] = C[i][j][k];
}
}
}
printf("3D Array is:\t");
PrintDouble3D(ptr, dim1, dim2, dim3);
printf("---------------------------------------------------\n");
for (int i = 0; i < dim1; ++i) {
for (int j = 0; j < dim2; ++j) {
free(ptr[i][j]);
}
free(ptr[i]);
}
free(ptr);
return 0;
}