Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React UI: Changing color for a shape #1194

Merged
merged 6 commits into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions cvat-canvas/src/typescript/canvasView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,9 +464,10 @@ export class CanvasViewImpl implements CanvasView, Listener {
const circle: SVG.Circle = this.nested
.circle(this.options.pointSize)
.stroke('black')
.fill(shape.node.getAttribute('fill') || 'inherit')
.fill('inherit')
.center(cx, cy)
.attr({
'fill-opacity': 1,
'stroke-width': consts.POINTS_STROKE_WIDTH / self.geometry.scale,
});

Expand Down Expand Up @@ -496,6 +497,11 @@ export class CanvasViewImpl implements CanvasView, Listener {
deepSelect: true,
});
}

const handler = shape.remember('_selectHandler');
if (handler && handler.nested) {
handler.nested.fill(shape.attr('fill'));
}
}

public constructor(model: CanvasModel & Master, controller: CanvasController) {
Expand Down Expand Up @@ -1314,10 +1320,7 @@ export class CanvasViewImpl implements CanvasView, Listener {
.addClass('cvat_canvas_shape').attr({
clientID: state.clientID,
id: `cvat_canvas_shape_${state.clientID}`,
fill: state.color,
'data-z-order': state.zOrder,
}).style({
'fill-opacity': 1,
});

group.bbox = basicPolyline.bbox.bind(basicPolyline);
Expand Down
2 changes: 1 addition & 1 deletion cvat-core/src/annotations-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@

if (updated.color) {
checkObjectType('color', data.color, 'string', null);
if (/^#[0-9A-F]{6}$/i.test(data.color)) {
if (!/^#[0-9A-F]{6}$/i.test(data.color)) {
throw new ArgumentError(
`Got invalid color value: "${data.color}"`,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,15 +302,12 @@ export default class CanvasWrapperComponent extends React.PureComponent<Props> {
// TODO: In this approach CVAT-UI know details of implementations CVAT-CANVAS (svg.js)
const shapeView = window.document.getElementById(`cvat_canvas_shape_${state.clientID}`);
if (shapeView) {
if (['rect', 'polygon', 'polyline'].includes(shapeView.tagName)) {
(shapeView as any).instance.fill({ color: shapeColor, opacity: opacity / 100 });
(shapeView as any).instance.stroke({ color: blackBorders ? 'black' : shapeColor });
} else {
// group of points
for (const child of (shapeView as any).instance.children()) {
child.fill({ color: shapeColor });
}
const handler = (shapeView as any).instance.remember('_selectHandler');
if (handler && handler.nested) {
handler.nested.fill({ color: shapeColor });
}
(shapeView as any).instance.fill({ color: shapeColor, opacity: opacity / 100 });
(shapeView as any).instance.stroke({ color: blackBorders ? 'black' : shapeColor });
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React from 'react';

import {
Row,
Col,
Button,
} from 'antd';

interface Props {
colors: string[];
onChange(color: string): void;
}

function ColorChanger(props: Props): JSX.Element {
const {
colors,
onChange,
} = props;

const cols = 6;
const rows = Math.ceil(colors.length / cols);

const antdRows = [];
for (let row = 0; row < rows; row++) {
const antdCols = [];
for (let col = 0; col < cols; col++) {
const idx = row * cols + col;
if (idx >= colors.length) {
break;
}
const color = colors[idx];
antdCols.push(
<Col key={col} span={4}>
<Button
onClick={(): void => onChange(color)}
style={{ background: color }}
className='cvat-label-item-color-button'
/>
</Col>,
);
}

antdRows.push(
// eslint-disable-next-line react/no-children-prop
<Row key={row} children={antdCols} />,
);
}

return (
<>
{antdRows}
</>
);
}

export default React.memo(ColorChanger);
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,7 @@ import {
} from 'antd';

import Text from 'antd/lib/typography/Text';

interface PopoverContentProps {
colors: string[];
changeColor(color: string): void;
}

function PopoverContent(props: PopoverContentProps): JSX.Element {
const {
colors,
changeColor,
} = props;

const cols = 6;
const rows = Math.ceil(colors.length / cols);

const antdRows = [];
for (let row = 0; row < rows; row++) {
const antdCols = [];
for (let col = 0; col < cols; col++) {
const idx = row * cols + col;
if (idx >= colors.length) {
break;
}
const color = colors[idx];
antdCols.push(
<Col key={col} span={4}>
<Button
onClick={(): void => changeColor(color)}
style={{ background: color }}
className='cvat-label-item-color-button'
/>
</Col>,
);
}

antdRows.push(
// eslint-disable-next-line react/no-children-prop
<Row key={row} children={antdCols} />,
);
}

return (
<>
{antdRows}
</>
);
}
import ColorChanger from 'components/annotation-page/standard-workspace/objects-side-bar/color-changer';

interface Props {
labelName: string;
Expand Down Expand Up @@ -99,8 +53,8 @@ function LabelItemComponent(props: Props): JSX.Element {
placement='left'
trigger='click'
content={(
<PopoverContent
changeColor={changeColor}
<ColorChanger
onChange={changeColor}
colors={labelColors}
/>
)}
Expand Down
Loading